<?php
/**
 *description:库内
 *author:李飞
 *time:2016/12/29
 */

namespace Admin\Controller;
use Think\Controller;
class StockController extends CommonController {
	//ajax获取入库单号
	public function ajax_otherinbound_orderno(){
		$t_no=serialNumber('OtherInbound');//流水号
		$order_no='OI'.date('Ymd',time()).$t_no;//流水单号
		$return['status']=1;
		$return['order_no']=$order_no;
		$this->ajaxReturn($return);
    }
	//ajax获取入库单批次号
	public function ajax_otherinbound_batchno(){
		// $t_no=serialNumber('OtherInbound');//流水号
		$batch_no='OI'.date('YmdHis',time());//流水单号
		$return['status']=1;
		$return['batch_no']=$batch_no;
		$this->ajaxReturn($return);
    }
	//ajax获取出库单号
	public function ajax_otheroutbound_orderno(){
		$t_no=serialNumber('OtherOutbound');//流水号
		$order_no='OI'.date('Ymd',time()).$t_no;//流水单号
		$return['status']=1;
		$return['order_no']=$order_no;
		$this->ajaxReturn($return);
    }
	//ajax获取盘库单号
	public function ajax_checkstock_orderno(){
		$t_no=serialNumber('Checkstock');//流水号
		$order_no='CS'.date('Ymd',time()).$t_no;//流水单号
		$return['status']=1;
		$return['order_no']=$order_no;
		$this->ajaxReturn($return);
    }
	//ajax获取调拨单号
	public function ajax_movedepot_orderno(){
		$t_no=serialNumber('MoveDepot');//流水号
		$order_no='MD'.date('Ymd',time()).$t_no;//流水单号
		$return['status']=1;
		$return['order_no']=$order_no;
		$this->ajaxReturn($return);
    }
	//ajax获取耗材入库单号
	public function ajax_material_orderno(){
		$t_no=serialNumber('PackageMaterialOrder');//流水号
		$order_no='MD'.date('Ymd',time()).$t_no;//流水单号
		$return['status']=1;
		$return['order_no']=$order_no;
		$this->ajaxReturn($return);
    }
    public function index(){
		$this->redirect('grounding');
    }
	public function goodslog(){
		$this->display();
    }
	//商品管理
	public function goods(){
		$this->display();
    }
	//耗材库存
	public function packagematerial(){
		$this->display();
	}
	//耗材账册
	public function packagematerialbooks(){
		$this->display();
	}
	//盘盈核增
	public function inventoryProfit(){
		
		$this->display();
	}
	//残次品入库
	public function scrap_in(){
		$this->display();
	}
	//盘亏核减
	public function inventoryLosses(){
		
		$this->display();
	}
	//残次品出库
	public function scrap_out(){
		$this->display();
	}
	//耗材管理
	public function material(){
		$t_no=serialNumber('OtherInbound');//流水号
		$order_no='MA'.date('Ymd',time()).$t_no;//流水单号
		
		$sectionlist=sectionlist();
		$personnellist=personnellist();
		$contactsCompanylist=contactsCompanylist();

		$this->assign('sectionlist',$sectionlist);
		$this->assign('personnellist',$personnellist);
		$this->assign('contactsCompanylist',$contactsCompanylist);
		$this->assign('order_no',$order_no);

		$this->display();
    }
	//耗材入库单详情
	public function materialInfo(){
		
		$this->display();
    }
	//商品上架
	public function grounding(){
		$m=M('Storage',C('DB_PRS'),C('DB_DEPOT'));
		$storage_list=$m->where(array('state'=>1))->select();
		$this->assign('storage_list',$storage_list);
		$this->display();
    }
	//最新的历史库位信息
	public function get_storagehistoryinfo(){
		$ao=M('ArrivalOrder',C('DB_PRS'),C('DB_DEPOT'));
		$m=M('Storage',C('DB_PRS'),C('DB_DEPOT'));
		$id=I('id');
		$ao_info=$ao->field('goods_no')->where(array('id'=>$id))->find();
		$ao_list=$ao->field('storage_code')->where(array('goods_no'=>$ao_info['goods_no'],"storage_code"=>array("exp","is not null")))->order('add_time desc')->select();
		if(empty($ao_list)){
			alert_json(0,'没有记录');
		}
		$storage_recent_info=$m->field('storage_code,name')->where(array('state'=>1,'storage_code'=>$ao_list[0]['storage_code']))->find();
		if(empty($storage_recent_info)){
			alert_json(0,'没有记录');
		}else{
			alert_json(1,$storage_recent_info);
		}
	}
	//库存预警
	public function get_goodslog(){
		$ck=A('CheckInput');
		//参数验证
		$gl=M('GoodsLog',C('DB_PRS'),C('DB_DEPOT'));
		$goods_no=trim(I('goods_no'));
		$bar_code=trim(I('bar_code'));
		$goods_name=trim(I('goods_name'));
		$start_time=trim(I('start_time'));
		$end_time=trim(I('end_time'));
		$need_stock=trim(I('need_stock'));
		// dump($need_stock);
		//搜索条件
		$map=array();
		
		$alert_storage_amount=C('base_config.alert_storage_amount');
		$time=time()-3600*24;//昨天的现在时间
		
		if(!empty($goods_no)){
			$map['goods_no']=array('like',"%$goods_no%");
		}
		if(!empty($bar_code)){
			$map['bar_code']=array('like',"%$bar_code%");
		}
		if(!empty($goods_name)){
			$map['goods_name']=array('like',"%$goods_name%");
		}
		
		!empty($start_time)?$start_time = strtotime($start_time):$start_time=0;
		!empty($end_time)?$end_time = strtotime($end_time):$end_time =time();
		
		$map['_string'] = "add_time>=$start_time AND add_time<=$end_time";	
		
		if($need_stock==1){	//需盘库
			$map['add_time']=array('gt',$time);
			$map['storage_amount']=array('elt',$alert_storage_amount);
		}
		
		if($need_stock==2){	//不需盘库
			$map['add_time']=array('elt',$time);
			$map['storage_amount']=array('gt',$alert_storage_amount);
		}
		
        $page=$ck->in('当前页','page','intval','1',0,0);
        $rows=$ck->in('每页记录数','rows','intval','',0,0);
		//排序
        $sort=$ck->in('排序','sort','cnennumstr','id',0,0);
        $order=$ck->in('规则','order','cnennumstr','desc',0,0);

		//获取数据
        $data=getTable('GoodsLog',$map,$page,$rows,$sort,$order,$status);
		// file_put_contents('./log/get_goodslog.log',date('Y-m-d H:i:s').var_export($gl->_sql(),true).PHP_EOL,FILE_APPEND);//日志导出sql
		// echo M()->_sql();
		// die;
		$excel=trim(I('excel'));
		
		foreach($data['rows'] as $k=>$v){
			if($v['storage_amount']<=$alert_storage_amount and strtotime($v['add_time'])>$time){
				$data['rows'][$k]['cz']="需盘库";
			}
			// echo $v['add_time'].'/';
			// echo $time.'<br>';
			$data['rows'][$k]['manua_id']=" ".$v['manua_id'];
			$data['rows'][$k]['goods_no']=" ".$v['goods_no'];
			$data['rows'][$k]['bar_code']=" ".$v['bar_code'];
			
			$data['rows'][$k]['total_hz_nums']=empty($v['total_hz_nums'])?0:$v['total_hz_nums'];
			$data['rows'][$k]['total_hj_nums']=empty($v['total_hj_nums'])?0:$v['total_hj_nums'];
			$data['rows'][$k]['has_grounding']=empty($v['has_grounding'])?0:$v['has_grounding'];
			// $data['rows'][$k]['total_inbound_nums']=empty($v['total_inbound_nums'])?0:$v['total_inbound_nums'];
			$data['rows'][$k]['storage_amount']=empty($v['storage_amount'])?0:$v['storage_amount'];
			$data['rows'][$k]['total_inbound_nums']=empty($v['total_inbound_nums'])?0:$v['total_inbound_nums'];
			$data['rows'][$k]['lilun_storage_amount']=$data['rows'][$k]['has_grounding']-$data['rows'][$k]['total_inbound_nums'];
			$data['rows'][$k]['cha_storage_amount']=$data['rows'][$k]['lilun_storage_amount']-$v['storage_amount'];
		}
		if(!empty($excel)){ //导出Excel
			$xlsName  = "库存预警";
			$xlsCell  = array(
				array('goods_no','商品编码'),
				array('manua_id','账册编号'),
				array('goods_name','商品名称'),
				array('bar_code','条形码'),
				array('storage_amount','实际剩余库存'),
				array('lilun_storage_amount','理论剩余库存'),
				array('cha_storage_amount','差额'),
				array('has_grounding','总实收已上架'),
				array('no_grounding','总实收未上架'),
				array('down_grounding','总实收已下架'),
				array('total_inbound_nums','总导单'),
				array('total_oubound_nums','总出库'),
				array('total_receive_nums','总接收'),
				array('total_delete_nums','总删单'),
				array('total_hz_nums','总盘盈核增'),
				array('total_hj_nums','总盘亏核减'),
				array('total_oubound_nums_no','剩余未出库'),
				array('freez_nums','剩余冻结'),
				array('inbound_nums','当日导单'),
				array('receive_nums','当日接收'),
				array('oubound_nums','当日出库'),
				array('pack_nums','当日打包'),
				array('weight_nums','当日称重'),
				array('delete_order_nums','当日删单'),
				array('hz_nums','当日盘盈核增'),
				array('hj_nums','当日盘亏核减'),
				array('add_time','统计时间'),
				array('cz','操作'),
			);
			exportExcel($xlsName,$xlsCell,$data['rows']);
			die;
		}
		
        $this->ajaxReturn($data);
	}
	//调拨单详情
	public function moveDepotInfo(){
		$this->display();
    }
	
	//商品移库记录
	public function moveStoragelist(){
		$this->display();
	}
	
	//仓库移库
	public function moveStorage(){
		$m=M('Storage',C('DB_PRS'),C('DB_DEPOT'));
		$storage_list=$m->select();
		$this->assign('storage_list',$storage_list);
		$this->display();
	}
	//保质期管理
	public function overdue(){
		$m=M('Storage',C('DB_PRS'),C('DB_DEPOT'));
		$storage_list=$m->where(array('state'=>1))->select();
		$this->assign('storage_list',$storage_list);
		$this->display();
	}
	
	//耗材入库单添加耗材详情
    public function materialInfoaddgoods(){
		$m=M('PackageMaterialOrderinfo',C('DB_PRS'),C('DB_DEPOT'));
		$n=M('PackageMaterialOrder',C('DB_PRS'),C('DB_DEPOT'));
        if(!IS_POST) exit;
        $ck=A('CheckInput');  
        $id=$ck->in('id','id','string','',1,0);
		$order_id=$ck->in('入库单id','order_id','intval','',1,0);
        if(empty($id)) exit;//为空
		$data_n['add_goods']=1;
		$data_m['order_id']=$order_id;
		$arr=explode(',', $id);
		$i=0;//新增
		$j=0;//失败
		$k=0;//重复
		foreach($arr as $key=>$v){
			$data_m['books_id']=$v;
			$detail=$m->where($data_m)->find();
			// echo $m->getlastsql();
			if($detail){
				$k++;
			}else{
				$result=$m->add($data_m);
				// echo $m->getlastsql();
				// die;
				if($result){
					$i++;
				}else{
					$j++;
				}
			}
		}
		if($i){
			$n->where(array('id'=>$order_id))->save($data_n);
			$message=$i.'个添加成功!';
		}
		if($j){
			$message=$j.'个添加失败!';
		}
		if($k){
			$message=$k.'个重复!';
		}
		$return['message']=$message;
        $return['status']=true;
        $this->ajaxReturn($return);
	}
	
	//其它出库单添加商品详情
    public function otherOutboundInfoaddgoods(){
		$m=M('OtherOutboundInfo',C('DB_PRS'),C('DB_DEPOT'));
		$n=M('OtherOutbound',C('DB_PRS'),C('DB_DEPOT'));
        if(!IS_POST) exit;
        $ck=A('CheckInput');  
        $id=$ck->in('id','id','string','',1,0);
		$order_id=$ck->in('其它出库单id','order_id','intval','',1,0);
        if(empty($id)) exit;//为空
		$data_n['add_goods']=1;
		$data_m['order_id']=$order_id;
		$n_info=$n->where(array('id'=>$order_id))->find();
		if($n_info['out_status']){
			$return['message']="已出库不能再添加商品！";
			$return['status']=false;
			$this->ajaxReturn($return);
		}
		$arr=explode(',', $id);
		$i=0;//新增
		$j=0;//失败
		$k=0;//重复
		foreach($arr as $key=>$v){
			$data_m['goods_id']=$v;
			$detail=$m->where($data_m)->find();
			// echo $m->getlastsql();
			if($detail){
				$k++;
			}else{
				$result=$m->add($data_m);
				// echo $m->getlastsql();
				// die;
				if($result){
					$i++;
				}else{
					$j++;
				}
			}
		}
		
		if($i){
			$n->where(array('id'=>$order_id))->save($data_n);
			$message=$i.'个添加成功!';
		}
		if($j){
			$message=$j.'个添加失败!';
		}
		if($k){
			$message=$k.'个重复!';
		}
		$return['message']=$message;
        $return['status']=true;
        $this->ajaxReturn($return);
	}
	//仓库调拨单添加商品详情
    public function moveDepotInfoaddgoods(){
		$m=M('MoveDepotInfo',C('DB_PRS'),C('DB_DEPOT'));
		$n=M('MoveDepot',C('DB_PRS'),C('DB_DEPOT'));
        if(!IS_POST) exit;
        $ck=A('CheckInput');  
        $id=$ck->in('id','id','string','',1,0);
		$order_id=$ck->in('调拨单id','order_id','intval','',1,0);
        if(empty($id)) exit;//为空
		$data_n['add_goods']=1;
		$data_m['order_id']=$order_id;
		$arr=explode(',', $id);
		$i=0;//新增
		$j=0;//失败
		$k=0;//重复
		foreach($arr as $key=>$v){
			$data_m['goods_id']=$v;
			$detail=$m->where($data_m)->find();
			// echo $m->getlastsql();
			if($detail){
				$k++;
			}else{
				$result=$m->add($data_m);
				// echo $m->getlastsql();
				// die;
				if($result){
					$i++;
				}else{
					$j++;
				}
			}
		}
		if($i){
			$n->where(array('id'=>$order_id))->save($data_n);
			$message=$i.'个添加成功!';
		}
		if($j){
			$message=$j.'个添加失败!';
		}
		if($k){
			$message=$k.'个重复!';
		}
		$return['message']=$message;
        $return['status']=true;
        $this->ajaxReturn($return);
	}
	//商品调拨
	public function moveDepot(){
		$t_no=serialNumber('MoveDepot');//流水号
		$order_no='MD'.date('Ymd',time()).$t_no;//流水单号
		$sectionlist=sectionlist();
		$personnellist=personnellist();
		$contactsCompanylist=contactsCompanylist();
		$depotlist=depot_list();

		$this->assign('sectionlist',$sectionlist);
		$this->assign('personnellist',$personnellist);
		$this->assign('contactsCompanylist',$contactsCompanylist);
		$this->assign('depotlist',$depotlist);
		$this->assign('order_no',$order_no);
		$this->display();
	}
	
	//耗材列表
	public function get_packagematerial(){
		$ck=A('CheckInput');
		//参数验证
        $pn=$ck->in('条形码','pn','ennumstr','',0,0);//条形码
		$name=I('name');
		
        $page=$ck->in('当前页','page','intval','1',0,0);
        $rows=$ck->in('每页记录数','rows','intval','',0,0);
		//排序
        $sort=$ck->in('排序','sort','cnennumstr','id',0,0);
        $order=$ck->in('规则','order','cnennumstr','desc',0,0);
		//搜索条件
		$map=array();
		
		!empty($pn)?$map['pn']=array('like','%'.$pn.'%'):'';
		!empty($name)?$map['name']=array('like','%'.$name.'%'):'';
		
		//获取数据
        $data=getTable('PackageMaterial',$map,$page,$rows,$sort,$order,$status);
		
        $this->ajaxReturn($data);
		
	}
	//批次库存商品
	public function get_ao_goods(){
		$ck=A('CheckInput');
		//参数验证
		$param=I('param.','','trim');
        $goods_name=$param['goods_name'];//商品名称
		$goods_no=$param['goods_no'];//商品编码
		
        $page=$ck->in('当前页','page','intval','1',0,0);
        $rows=$ck->in('每页记录数','rows','intval','',0,0);
		//排序
        $sort=$ck->in('排序','sort','cnennumstr','id',0,0);
        $order=$ck->in('规则','order','cnennumstr','desc',0,0);
		//搜索条件
		$map=array();
		$map['inbound_status']=1;//已入库
		
		!empty($goods_name)?$map['goods_name']=array('like','%'.$goods_name.'%'):'';
		!empty($goods_no)?$map['goods_no']=array('like','%'.$goods_no.'%'):'';
		$status=1;
		//获取数据
        $data=getTable('ArrivalOrder',$map,$page,$rows,$sort,$order,$status);
		
        $this->ajaxReturn($data);
	}
	//盘盈核增列表
	public function get_inventoryProfit(){
		$ck=A('CheckInput');
		$ao=M('ArrivalOrder',C('DB_PRS'),C('DB_DEPOT'));
		//参数验证
		$param=I('param.','','trim');
        $goods_name=$param['goods_name'];//商品名称
		$goods_no=$param['goods_no'];//商品编码
		
        $page=$ck->in('当前页','page','intval','1',0,0);
        $rows=$ck->in('每页记录数','rows','intval','',0,0);
		//排序
        $sort=$ck->in('排序','sort','cnennumstr','id',0,0);
        $order=$ck->in('规则','order','cnennumstr','desc',0,0);
		//搜索条件
		$map=array();

		$map['cate']=$param['cate'];//1核增2核减
		!empty($goods_name)?$map['goods_name']=array('like','%'.$goods_name.'%'):'';
		!empty($goods_no)?$map['goods_no']=array('like','%'.$goods_no.'%'):'';
		$status=I('status');
		//获取数据
        $data=getTable('CheckstockLog',$map,$page,$rows,$sort,$order,$status);
		foreach($data['rows'] as $k=>$v){
			$data['rows'][$k]['goods_order_no']=$ao->where(array('id'=>$v['goods_id']))->getField('order_no');
		}
        $this->ajaxReturn($data);
	}
	//分销库存
	public function distributionLog(){
		$b=M('BooksCate',C('DB_PRS'),C('DB_DEPOT'));
		$merchant_codelist=$b->select();
		
		$this->assign('merchant_codelist',$merchant_codelist);
		$this->display();
	}
	//分销库存列表
	public function get_distributionLog(){
		$ck=A('CheckInput');
		$ao=M('ArrivalOrder',C('DB_PRS'),C('DB_DEPOT'));
		$b=M('Books',C('DB_PRS'),C('DB_DEPOT'));
		//参数验证
		$param=I('param.','','trim');
        $goods_name=$param['goods_name'];//商品名称
		$goods_no=$param['goods_no'];//商品编码
		
        $page=$ck->in('当前页','page','intval','1',0,0);
        $rows=$ck->in('每页记录数','rows','intval','',0,0);
		//排序
        $sort=$ck->in('排序','sort','cnennumstr','id',0,0);
        $order=$ck->in('规则','order','cnennumstr','desc',0,0);
		//搜索条件
		$map=array();

		$map['cate']=$param['cate'];//1核增2核减
		!empty($goods_name)?$map['goods_name']=array('like','%'.$goods_name.'%'):'';
		!empty($goods_no)?$map['goods_no']=array('like','%'.$goods_no.'%'):'';
		$status=I('status');
		//获取数据
        $data=getTable('DistributionLog',$map,$page,$rows,$sort,$order,$status);
		foreach($data['rows'] as $k=>$v){
			$data['rows'][$k]['goods_name']=$b->where(array('goods_no'=>$v['goods_no']))->getField('goods_name');
		}
        $this->ajaxReturn($data);
	}
	//盘亏核减列表
	public function get_inventoryLosses(){
		$ck=A('CheckInput');
		$ao=M('ArrivalOrder',C('DB_PRS'),C('DB_DEPOT'));
		//参数验证
		$param=I('param.','','trim');
        $goods_name=$param['goods_name'];//商品名称
		$goods_no=$param['goods_no'];//商品编码
		
        $page=$ck->in('当前页','page','intval','1',0,0);
        $rows=$ck->in('每页记录数','rows','intval','',0,0);
		//排序
        $sort=$ck->in('排序','sort','cnennumstr','id',0,0);
        $order=$ck->in('规则','order','cnennumstr','desc',0,0);
		//搜索条件
		$map=array();
		
		$map['cate']=2;//核减
		!empty($goods_name)?$map['goods_name']=array('like','%'.$goods_name.'%'):'';
		!empty($goods_no)?$map['goods_no']=array('like','%'.$goods_no.'%'):'';
		$status=I('status');
		//获取数据
        $data=getTable('CheckstockLog',$map,$page,$rows,$sort,$order,$status);
		foreach($data['rows'] as $k=>$v){
			$data['rows'][$k]['goods_order_no']=$ao->where(array('id'=>$v['goods_id']))->getField('order_no');
		}
        $this->ajaxReturn($data);
	}
	//残次品入库列表
	public function get_scrap_in(){
		$ck=A('CheckInput');
		$ao=M('ArrivalOrder',C('DB_PRS'),C('DB_DEPOT'));
		//参数验证
		$param=I('param.','','trim');
        $goods_name=$param['goods_name'];//商品名称
		$goods_no=$param['goods_no'];//商品编码
		
        $page=$ck->in('当前页','page','intval','1',0,0);
        $rows=$ck->in('每页记录数','rows','intval','',0,0);
		//排序
        $sort=$ck->in('排序','sort','cnennumstr','id',0,0);
        $order=$ck->in('规则','order','cnennumstr','desc',0,0);
		//搜索条件
		$map=array();

		$map['cate']=1;//1入库2出库
		!empty($goods_name)?$map['goods_name']=array('like','%'.$goods_name.'%'):'';
		!empty($goods_no)?$map['goods_no']=array('like','%'.$goods_no.'%'):'';
		$status=I('status');
		//获取数据
        $data=getTable('CheckstockScrap',$map,$page,$rows,$sort,$order,$status);
		foreach($data['rows'] as $k=>$v){
			$data['rows'][$k]['goods_order_no']=$ao->where(array('id'=>$v['goods_id']))->getField('order_no');
		}
        $this->ajaxReturn($data);
	}
	//残次品出库列表
	public function get_scrap_out(){
		$ck=A('CheckInput');
		$ao=M('ArrivalOrder',C('DB_PRS'),C('DB_DEPOT'));
		//参数验证
		$param=I('param.','','trim');
        $goods_name=$param['goods_name'];//商品名称
		$goods_no=$param['goods_no'];//商品编码
		
        $page=$ck->in('当前页','page','intval','1',0,0);
        $rows=$ck->in('每页记录数','rows','intval','',0,0);
		//排序
        $sort=$ck->in('排序','sort','cnennumstr','id',0,0);
        $order=$ck->in('规则','order','cnennumstr','desc',0,0);
		//搜索条件
		$map=array();
		
		$map['cate']=2;//核减
		!empty($goods_name)?$map['goods_name']=array('like','%'.$goods_name.'%'):'';
		!empty($goods_no)?$map['goods_no']=array('like','%'.$goods_no.'%'):'';
		$status=I('status');
		//获取数据
        $data=getTable('CheckstockScrap',$map,$page,$rows,$sort,$order,$status);
		foreach($data['rows'] as $k=>$v){
			$data['rows'][$k]['goods_order_no']=$ao->where(array('id'=>$v['goods_id']))->getField('order_no');
		}
        $this->ajaxReturn($data);
	}
	//库位列表
	public function get_storagelist(){
		$ck=A('CheckInput');
		//参数验证
        $storage_code=$ck->in('库位编码','storage_code','ennumstr','',0,0);//条形码
		$name=I('name');
		
        $page=$ck->in('当前页','page','intval','1',0,0);
        $rows=$ck->in('每页记录数','rows','intval','',0,0);
		//排序
        $sort=$ck->in('排序','sort','cnennumstr','id',0,0);
        $order=$ck->in('规则','order','cnennumstr','desc',0,0);
		//搜索条件
		$map=array();
		
		!empty($storage_code)?$map['storage_code']=array('like','%'.$storage_code.'%'):'';
		!empty($name)?$map['name']=array('like','%'.$name.'%'):'';
		$status=I('status');
		//获取数据
        $data=getTable('Storage',$map,$page,$rows,$sort,$order,$status);
		
        $this->ajaxReturn($data);
		
	}
	//耗材账册列表
	public function get_packagematerialbooks(){
		$ck=A('CheckInput');
		//参数验证
        $pn=$ck->in('条形码','pn','ennumstr','',0,0);//条形码
		$name=I('name');
		
        $page=$ck->in('当前页','page','intval','1',0,0);
        $rows=$ck->in('每页记录数','rows','intval','',0,0);
		//排序
        $sort=$ck->in('排序','sort','cnennumstr','id',0,0);
        $order=$ck->in('规则','order','cnennumstr','desc',0,0);
		//搜索条件
		$map=array();
		
		!empty($pn)?$map['pn']=array('like','%'.$pn.'%'):'';
		!empty($name)?$map['name']=array('like','%'.$name.'%'):'';
		$status=I('status');
		//获取数据
        $data=getTable('PackageMaterialBooks',$map,$page,$rows,$sort,$order,$status);
        $this->ajaxReturn($data);
	}
	
	//耗材入库单详情
	public function get_materialInfo(){
		$m=M('PackageMaterialOrderinfo',C('DB_PRS'),C('DB_DEPOT'));
        $ck=A('CheckInput');
		//参数验证
        $page=$ck->in('当前页','page','intval','1',0,0);
        $rows=$ck->in('每页记录数','rows','intval','',0,0);
		//排序
        $sort=$ck->in('排序','sort','cnennumstr','id',0,0);
        $order=$ck->in('规则','order','cnennumstr','desc',0,0);
		$order_id=I('order_id');
		$map['m.order_id']=$order_id;
		$count=$m->alias('m')->join('__PACKAGE_MATERIAL_BOOKS__ as n on m.books_id=n.id')->where($map)->count();
        $info=$m->alias('m')->join('__PACKAGE_MATERIAL_BOOKS__ as n on m.books_id=n.id')->field('m.id,m.order_id,m.books_id,m.nums,n.pn,n.name,n.l_w_h')->where($map)->page($page,$rows)->order($sort.' '.$order)->select();
		// echo $m->getlastsql();
		// dump($info);
		//获取数据
        $data['total']=$count;
        $data['rows']=$info;
		
        $this->ajaxReturn($data);
	}
	//耗材入库单详情
	public function get_material(){
		$ck=A('CheckInput');
		//参数验证
        $order_no=$ck->in('单号','order_no','ennumstr','',0,0);//单号
		$people_purchasing_no=I('people_purchasing_no');
		$people_make_no=I('people_make_no');
		
        $page=$ck->in('当前页','page','intval','1',0,0);
        $rows=$ck->in('每页记录数','rows','intval','',0,0);
		$status=I('status');
		//排序
        $sort=$ck->in('排序','sort','cnennumstr','id',0,0);
        $order=$ck->in('规则','order','cnennumstr','desc',0,0);
		//搜索条件
		$map=array();
		
		!empty($order_no)?$map['order_no']=array('like','%'.$order_no.'%'):'';
		!empty($people_purchasing_no)?$map['people_purchasing_no']=array('like','%'.$people_purchasing_no.'%'):'';
		!empty($people_make_no)?$map['people_make_no']=array('like','%'.$people_make_no.'%'):'';
		
		//获取数据
        $data=getTable('PackageMaterialOrder',$map,$page,$rows,$sort,$order,$status);
		
        $this->ajaxReturn($data);
	}

	//调拨单详情记录
	public function get_moveDepotInfo(){
		$m=M('MoveDepotInfo',C('DB_PRS'),C('DB_DEPOT'));
        $ck=A('CheckInput');
		//参数验证
        $page=$ck->in('当前页','page','intval','1',0,0);
        $rows=$ck->in('每页记录数','rows','intval','',0,0);
		//排序
        $sort=$ck->in('排序','sort','cnennumstr','id',0,0);
        $order=$ck->in('规则','order','cnennumstr','desc',0,0);
		$order_id=I('order_id');
		$map['m.order_id']=$order_id;
		$count=$m->alias('m')->join('__ARRIVAL_ORDER__ as n on m.goods_id=n.id')->where($map)->count();
        $info=$m->alias('m')->join('__ARRIVAL_ORDER__ as n on m.goods_id=n.id')->field('m.id,m.order_id,m.goods_id,m.nums,n.goods_no,n.manual_id as manua_id,n.goods_name,n.goods_spec,n.currency as current_type,n.country_code,n.unit1')->where($map)->page($page,$rows)->order($sort.' '.$order)->select();
		// echo $m->getlastsql();
		// dump($info);
		//获取数据
        $data['total']=$count;
        $data['rows']=$info;
		
        $this->ajaxReturn($data);
	}
	
	//获得商品移库记录
	public function get_movestoragelist(){
		$m=M('StorageMove',C('DB_PRS'),C('DB_DEPOT'));
		$unit=M('Unit',C('DB_PRS'),C('DB_DEPOT'));
        $ck=A('CheckInput');

        //搜索条件
        $map=array();
        $goods_no=$ck->in('商品编码','goods_no','ennumstr','',0,0);        

        !empty($goods_no)?$map['g.goods_no']=array('like','%'.$goods_no.'%'):'';

        $page=$ck->in('当前页','page','intval','1',0,0);   
        $rows=$ck->in('每页记录数','rows','intval','',0,0);  

        //排序
        $sort=$ck->in('排序','sort','cnennumstr','id',0,0);
        $order=$ck->in('规则','order','cnennumstr','desc',0,0);
        
        $count=$m->alias('m')->join("__ARRIVAL_ORDER__ as g on g.id=m.goods_id ")->field('m.*,g.goods_no,g.goods_name,g.manual_id,g.source_no,g.unit1,g.unit2,g.bar_code')->where($map)->count();
        if(empty($map)){
            $info=$m->alias('m')->join("__ARRIVAL_ORDER__ as g on g.id=m.goods_id ")->field('m.*,g.goods_no,g.goods_name,g.manual_id,g.source_no,g.unit1,g.unit2,g.bar_code')->page($page,$rows)->order('g.'.$sort.' '.$order)->select();
        }else{
            $info=$m->alias('m')->join("__ARRIVAL_ORDER__ as g on g.id=m.goods_id ")->field('m.*,g.goods_no,g.goods_name,g.manual_id,g.source_no,g.unit1,g.unit2,g.bar_code')->where($map)->order('g.'.$sort.' '.$order)->select();
        }
		// dump($info);
		// die;
		// echo $m->getlastsql();
		// die;
		foreach($info as $k=>$v){
			$info_uint1=$unit->where(array('unit_no'=>$v['unit1']))->find();
			$info[$k]['unit1']=$info_uint1['unit_name'];
			$info_uint2=$unit->where(array('unit_no'=>$v['unit2']))->find();
			$info[$k]['unit2']=$info_uint2['unit_name'];
			$info[$k]['add_time']=date("Y-m-d H:i:s",$v['add_time']);
		}
        if(!empty($info)){
            $data['total']=$count;
            $data['rows']=$info;
        }else{
            $data['total']=0;
            $data['rows']=0;
        }
		
        $this->ajaxReturn($data);
		// $this->display();
	}
	//库存商品列表
	public function get_goodslist(){
		$ao=M('ArrivalOrder',C('DB_PRS'),C('DB_DEPOT'));
        $m=M('Goods',C('DB_PRS'),C('DB_DEPOT'));
		$storage=M('Storage',C('DB_PRS'),C('DB_DEPOT'));
        $ck=A('CheckInput');

        //搜索条件
        $map=array();
		$param=I('param.','','trim');
        $goods_no=$param['goods_no'];        
        $goods_name=$param['goods_name'];    
		$bar_code=$param['bar_code'];    
		
		$map['m.status']=1;
		$map['a.inbound_status']=1;
        !empty($goods_no)?$map['m.goods_no']=array('like','%'.$goods_no.'%'):'';
		!empty($goods_name)?$map['m.goods_name']=array('like','%'.$goods_name.'%'):'';
		!empty($bar_code)?$map['m.bar_code']=array('like','%'.$bar_code.'%'):'';
		
		$map['a.grounding_status']=1;
		$goods_cate=I('goods_cate');
		if(!empty($goods_cate)){
			if($goods_cate==1000){
				$map['m.goods_cate']=array('neq',1);
			}else{
				$map['m.goods_cate']=$goods_cate;
			}
			
		}
		// dump(I());
        $page=$ck->in('当前页','page','intval','1',0,0);   
        $rows=$ck->in('每页记录数','rows','intval','',0,0);  

        //排序
        $sort=$ck->in('排序','sort','cnennumstr','id',0,0);
        $order=$ck->in('规则','order','cnennumstr','desc',0,0);

		$info=$m->alias('m')->field('m.*,count(a.freez_amount) info_count,sum(a.freez_amount) freez_amount,sum(a.storage_amount) storage_amount')->join("__ARRIVAL_ORDER__ a on m.goods_no=a.goods_no and m.manua_id=a.manual_id","left")->group('m.goods_no,m.manua_id')->page($page,$rows)->where($map)->order('m.'.$sort.' '.$order)->select();
		// echo $m->getlastsql();

		$info_list=$m->alias('m')->field('m.*,count(a.freez_amount) info_count,sum(a.freez_amount) freez_amount,sum(a.storage_amount) storage_amount')->join("__ARRIVAL_ORDER__ a on m.goods_no=a.goods_no and m.manua_id=a.manual_id","left")->group('m.goods_no,m.manua_id')->where($map)->select();
		$count=count($info_list);

		$excel=I('excel');
		if(!empty($excel)){ //导出Excel
			foreach($info as $k=>$v){
				$ao_list=$ao->field('storage_code')->where(array('goods_no'=>$v['goods_no'],'storage_amount'=>array('GT',0)))->select();
				$storage_name='';
				foreach($ao_list as $ks=>$vs){
					$storage_name='/'.$storage->where(array('storage_code'=>$vs['storage_code']))->getField('name');
				}
				$info[$k]['storage_name']=substr($storage_name,1)?substr($storage_name,1):'';
				$info[$k]['push_time']=date("Y-m-d H:i:s",$v['push_time']);
				$info[$k]['goods_no']=' '.$v['goods_no'];
				$info[$k]['manua_id']=' '.$v['manua_id'];
				$info[$k]['bar_code']=" ".$v['bar_code'];
				if($v['grounding_status']==0){
					$info[$k]['grounding_status']='否';
				}
				if($v['grounding_status']==1){
					$info[$k]['grounding_status']='是';
				}
			}
		}else{
			foreach($info as $k=>$v){
				$info[$k]['push_time']=date("Y-m-d H:i:s",$v['push_time']);
				$info[$k]['bad_product']=$ao->where(array('goods_no'=>$v['goods_no']))->getField('bad_product');//不良品
				if($v['grounding_status']==0){
					$info[$k]['grounding_status']='否';
				}
				if($v['grounding_status']==1){
					$info[$k]['grounding_status']='是';
				}
			}
		}
		
		if(!empty($excel)){ //导出Excel
			$xlsName  = "商品列表";
			$xlsCell  = array(
				array('goods_no','商品编码'),
				array('manua_id','账册编号'),
				array('goods_name','商品名称'),
				array('bar_code','条形码'),
				array('goods_price','平均价格'),
				array('amount','总数量'),
				array('storage_amount','库存数量'),
				array('conforming_p','合格品'),
				array('nonconforming_p','残次品'),
				array('scrap_one','报废品'),
				array('storage_name','库位'),
				array('freez_amount','冻结库存')
			);
			exportExcel($xlsName,$xlsCell,$info);
			die;
		}
        if(!empty($info)){
            $data['total']=$count;
            $data['rows']=$info;
        }else{
            $data['total']=0;
            $data['rows']=0;
        }
        $this->ajaxReturn($data);
    }
	
	
	//批次商品信息
	public function get_goods(){
        $m=M('ArrivalOrder',C('DB_PRS'),C('DB_DEPOT'));
        $ck=A('CheckInput');

        //搜索条件
        $map=array();
		$map['storage_amount']=array('GT',0);
        $goods_no=$ck->in('商品编码','goods_no','ennumstr','',0,0);        
        $goods_name=$ck->in('商品名称','goods_name','cnennumstr','',0,0);
		$bar_code=$ck->in('条形码','bar_code','ennumstr','',0,0);        
		
        !empty($goods_no)?$map['goods_no']=array('like','%'.$goods_no.'%'):'';
		!empty($goods_name)?$map['goods_name']=array('like','%'.$goods_name.'%'):'';
		!empty($bar_code)?$map['bar_code']=array('like','%'.$bar_code.'%'):'';
		
		$goods_cate=I('goods_cate');
		if(!empty($goods_cate)){
			$map['goods_cate']=$goods_cate;
		}
		
        $page=$ck->in('当前页','page','intval','1',0,0);   
        $rows=$ck->in('每页记录数','rows','intval','',0,0);  

        //排序
        $sort=$ck->in('排序','sort','cnennumstr','id',0,0);
        $order=$ck->in('规则','order','cnennumstr','desc',0,0);
        
        $count=$m->where($map)->count();
        if(empty($map)){
            $info=$m->page($page,$rows)->order($sort.' '.$order)->select();
        }else{
            $info=$m->where($map)->order($sort.' '.$order)->select();
        }
		foreach($info as $k=>$v){
			if(!empty($v['push_time'])){
				$info[$k]['push_time']=date("Y-m-d H:i:s",$v['push_time']);
			}

			if($v['grounding_status']==0){
				$info[$k]['grounding_status']='否';
			}
			if($v['grounding_status']==1){
				$info[$k]['grounding_status']='是';
			}
		}
        if(!empty($info)){
            $data['total']=$count;
            $data['rows']=$info;
        }else{
            $data['total']=0;
            $data['rows']=0;
        }
		
        $this->ajaxReturn($data);
    }
	//编辑商品确认
    public function confirm_editgoods(){
		$m=M('Goods',C('DB_PRS'),C('DB_DEPOT'));
		$ao=M('ArrivalOrder',C('DB_PRS'),C('DB_DEPOT'));
        if(!IS_POST) exit; 

		$data=I('param.','','trim');
		
        $map['id']=$data['id'];
		unset($data['id']);
		$goods_info=$m->where($map)->find();//获取商品详情
		
        $result=$m->where($map)->save($data);
		
        if(false===$result){
            $return['message']='保存失败';
            $return['status']=false;
            $this->ajaxReturn($return);
        }else{
			$data_ao['goods_cate']=$data['goods_cate'];
			$ress=$ao->where(array('goods_no'=>$goods_info['goods_no'],'manual_id'=>$goods_info['manua_id']))->save($data_ao);//更新批次信息
			// echo $ao->getlastsql();
			if($ress===false){
				$return['message']='更新批次信息失败!';
				$return['status']=false;
				$this->ajaxReturn($return);
			}
			$return['message']='保存成功!';
			$return['status']=true;
			$this->ajaxReturn($return);
        }
    }
	//确认移库
    public function confirm_moveStorage(){
		$m=M('ArrivalOrder',C('DB_PRS'),C('DB_DEPOT'));
        if(!IS_POST) exit; 
        $ck=A('CheckInput');
		$data['storage_code']=$ck->in('库位','storage_code','ennumstr','',1,10);
		$data['grounding_status']=1;
        $map['id']=$ck->in('id','id','intval','',1,0);
		//获取原商品信息
		$goods=$m->where($map)->find();
		
        $result=$m->where($map)->save($data);

        if(false===$result){
            $return['message']='移库失败';
            $return['status']=false;
            $this->ajaxReturn($return);
        }else{
			$res['goods_id']=$goods['id'];
			$res['history_storage_code']=$goods['storage_code'];
			$res['new_storage_code']=$data['storage_code'];
			$res['add_time']=time();
			$res=M('StorageMove',C('DB_PRS'),C('DB_DEPOT'))->add($res);
			if($res){
				$return['message']='移库成功!';
				$return['status']=true; 
				$this->ajaxReturn($return);
			}else{
				$return['message']='移库成功，保存移库记录失败!';
				$return['status']=true;
				$this->ajaxReturn($return);
			}
			
        }
    }
	//确认上架
    public function confirm_grounding(){
		$m=M('ArrivalOrder',C('DB_PRS'),C('DB_DEPOT'));
        if(!IS_POST) exit; 
        $ck=A('CheckInput');
        // $data['bar_code']=$ck->in('条形码','bar_code','ennumstr','',2,30);
		// $data['price']=$ck->in('价格','price','ennumstr','',1,30);
		$data['storage_code']=$ck->in('库位','storage_code','ennumstr','',1,10);
		
		$data['t_arrival_time']=strtotime(I('t_arrival_time'));
		$data['arrival_time']=strtotime(I('t_arrival_time'));
		$data['overdue_time']=strtotime(I('overdue_time'));
		$data['conforming_p']=I('conforming_p');
		$data['nonconforming_p']=I('nonconforming_p');
		$data['scrap_one']=I('scrap_one');
		
		$data['area']=I('area');
		$data['grounding_status']=1;
        $map['id']=$ck->in('id','id','intval','',1,0);
		
		$total_nums=$data['conforming_p']+$data['nonconforming_p']+$data['scrap_one'];
		$receive_amount=$m->where($map)->getField('receive_amount');
		if($receive_amount!=$total_nums){
			$return['message']='合格品+残次品+报废品不等于实际到货数量';
            $return['status']=false;
            $this->ajaxReturn($return);
		}
		
        $result=$m->where($map)->save($data);
		// echo $m->getlastsql();
        if(false===$result){
            $return['message']='上架失败';
            $return['status']=false;
            $this->ajaxReturn($return);
        }else{
			//按企业编码扣费--begin
			out_money(9,1,16);//9：到货商品清点
			out_money(10,1,16);//10：商品验收入库
			out_money(11,1,16);//11：商品上架
			out_money(28,1,16);//28：收件、上架、拣货、包装(跨境)
			out_money(29,1,16);//29：包装材料(跨境)
			//扣费--end
			$return['message']='上架成功!';
			$return['status']=true; 
			$this->ajaxReturn($return);
        }
    }
	//批量下架
    public function ungrounding(){
		$m=M('ArrivalOrder',C('DB_PRS'),C('DB_DEPOT'));
        if(!IS_POST) exit; 
        $ck=A('CheckInput'); 
        $id=$ck->in('库存商品id','id','string','',1,0);
        if(empty($id)) exit;//为空
        $arr=explode(',', $id);
        $map['id']=array('in',$arr);
		$data['storage_code']=0;
		$data['grounding_status']=0;
        $result=$m->where($map)->save($data);
        if($result===false){
            $return['message']='下架失败!';
            $return['status']=false;  
        }else{
            $return['message']='下架成功!';
            $return['status']=true; 
        }        
        $this->ajaxReturn($return);
    }
	//其它入库
    public function otherInbound(){
		$t_no=serialNumber('OtherInbound');//流水号
		$order_no='OI'.date('Ymd',time()).$t_no;//流水单号
		$sectionlist=sectionlist();
		$personnellist=personnellist();
		$contactsCompanylist=contactsCompanylist();
		$boundcatelist=boundcatelist();
		$this->assign('sectionlist',$sectionlist);
		$this->assign('personnellist',$personnellist);
		$this->assign('contactsCompanylist',$contactsCompanylist);
		$this->assign('boundcatelist',$boundcatelist);
		$this->assign('order_no',$order_no);
		$this->display();
	}
	//其它出库
    public function otherOutbound(){
		$t_no=serialNumber('OtherOutbound');//流水号
		$order_no='OT'.date('Ymd',time()).$t_no;//流水单号
		$sectionlist=sectionlist();
		$personnellist=personnellist();
		$contactsCompanylist=contactsCompanylist();
		$boundcatelist=boundcatelist(2);
		$this->assign('sectionlist',$sectionlist);
		$this->assign('personnellist',$personnellist);
		$this->assign('contactsCompanylist',$contactsCompanylist);
		$this->assign('boundcatelist',$boundcatelist);
		$this->assign('order_no',$order_no);
		$this->display();
	}
	//获取调拨单数据
    public function get_movedepot(){
		// dump(I());
		$ck=A('CheckInput');
		//参数验证
        $order_no=$ck->in('单号','order_no','ennumstr','',0,0);//单号
        $depot_code=$ck->in('仓库编码','depot_code','ennumstr','',0,0);//出入库类别
		$people_purchasing_no=I('people_purchasing_no');
		$people_make_no=I('people_make_no');
		
        $page=$ck->in('当前页','page','intval','1',0,0);
        $rows=$ck->in('每页记录数','rows','intval','',0,0);
		$status=I('status');
		//排序
        $sort=$ck->in('排序','sort','cnennumstr','id',0,0);
        $order=$ck->in('规则','order','cnennumstr','desc',0,0);
		//搜索条件
		$map=array();
		
		!empty($order_no)?$map['order_no']=array('like','%'.$order_no.'%'):'';
        !empty($depot_code)?$map['depot_code']=array('like','%'.$depot_code.'%'):'';
		!empty($people_purchasing_no)?$map['people_purchasing_no']=array('like','%'.$people_purchasing_no.'%'):'';
		!empty($people_make_no)?$map['people_make_no']=array('like','%'.$people_make_no.'%'):'';
		
		//获取数据
        $data=getTable('MoveDepot',$map,$page,$rows,$sort,$order,$status);
		
        $this->ajaxReturn($data);
	}
	//获取其它入库数据
    public function get_otherInbound(){
		// dump(I());
		$ck=A('CheckInput');
		//参数验证
        $order_no=$ck->in('单号','order_no','ennumstr','',0,0);//单号
        $bound_cate_code=$ck->in('出入库类别','bound_cate_code','cnennumstr','',0,0);//出入库类别
		$people_purchasing_no=I('people_purchasing_no');
		$people_make_no=I('people_make_no');
		
        $page=$ck->in('当前页','page','intval','1',0,0);
        $rows=$ck->in('每页记录数','rows','intval','',0,0);
		$status=I('status');
		//排序
        $sort=$ck->in('排序','sort','cnennumstr','id',0,0);
        $order=$ck->in('规则','order','cnennumstr','desc',0,0);
		//搜索条件
		$map=array();
		
		!empty($order_no)?$map['order_no']=array('like','%'.$order_no.'%'):'';
        !empty($bound_cate_code)?$map['bound_cate_code']=array('like','%'.$bound_cate_code.'%'):'';
		!empty($people_purchasing_no)?$map['people_purchasing_no']=array('like','%'.$people_purchasing_no.'%'):'';
		!empty($people_make_no)?$map['people_make_no']=array('like','%'.$people_make_no.'%'):'';
		
		//获取数据
        $data=getTable('OtherInbound',$map,$page,$rows,$sort,$order,$status);
		
        $this->ajaxReturn($data);
	}
	//获取其它出库数据
    public function get_otherOutbound(){
		// dump(I());
		$ck=A('CheckInput');
		//参数验证
        $order_no=$ck->in('单号','order_no','ennumstr','',0,0);//单号
        $bound_cate_code=$ck->in('出入库类别','bound_cate_code','cnennumstr','',0,0);//出入库类别
		$people_purchasing_no=I('people_purchasing_no');
		$people_make_no=I('people_make_no');
		
        $page=$ck->in('当前页','page','intval','1',0,0);
        $rows=$ck->in('每页记录数','rows','intval','',0,0);
		$status=I('status');
		//排序
        $sort=$ck->in('排序','sort','cnennumstr','id',0,0);
        $order=$ck->in('规则','order','cnennumstr','desc',0,0);
		//搜索条件
		$map=array();
		
		!empty($order_no)?$map['order_no']=array('like','%'.$order_no.'%'):'';
        !empty($bound_cate_code)?$map['bound_cate_code']=array('like','%'.$bound_cate_code.'%'):'';
		!empty($people_purchasing_no)?$map['people_purchasing_no']=array('like','%'.$people_purchasing_no.'%'):'';
		!empty($people_make_no)?$map['people_make_no']=array('like','%'.$people_make_no.'%'):'';
		
		//获取数据
        $data=getTable('OtherOutbound',$map,$page,$rows,$sort,$order,$status);
		
        $this->ajaxReturn($data);
	}
	//盘盈核增添加
	public function checkstocklogAdd(){
		$m=M('CheckstockLog',C('DB_PRS'),C('DB_DEPOT'));
		$ao=M('ArrivalOrder',C('DB_PRS'),C('DB_DEPOT'));
        if(!IS_POST) exit;
		$param=I('param.','','trim');
		$ids=$param['ids'];
		$ids=explode(',',$ids);
		$map_ao['id']=array('in',$ids);
		$ao_list=$ao->field('id,goods_no,goods_name,order_no')->where($map_ao)->select();
		
		$t_no=serialNumber('CheckstockLog');//流水号
		$order_no='CL'.date('Ymd',time()).$t_no;//流水单号
		
        foreach($ao_list as $k=>$v){
			$data[$k]['goods_id']=$v['id'];
			$data[$k]['goods_no']=$v['goods_no'];
			$data[$k]['goods_name']=$v['goods_name'];
			$data[$k]['order_no']=$order_no;
			$data[$k]['add_time']=time();
			$data[$k]['cate']=$param['cate'];
		}
        $insert=$m->addAll($data);
        if(!$insert){
            $return['message']='添加失败';
            $return['status']=0;
            $this->ajaxReturn($return);
        }else{
            $return['message']='添加成功!';
			$return['status']=1; 
			$this->ajaxReturn($return);
        }
	}
	//库内调拨添加
	public function distributionLogAdd(){
		$m=M('DistributionLog',C('DB_PRS'),C('DB_DEPOT'));
		$ao=M('ArrivalOrder',C('DB_PRS'),C('DB_DEPOT'));
        if(!IS_POST) exit;
		$param=I('param.','','trim');
		$merchant_code=$param['merchant_code'];
		if(!$merchant_code){
			$return['message']='请选择货主';
            $return['status']=0;
            $this->ajaxReturn($return);
		}
		
		$ids=$param['ids'];
		$ids=explode(',',$ids);
		$map_ao['id']=array('in',$ids);
		$ao_list=$ao->field('id,goods_no,goods_name,order_no')->where($map_ao)->select();
		
		$t_no=serialNumber('DistributionLog');//流水号
		$order_no='DL'.date('Ymd',time()).$t_no;//流水单号
		
        foreach($ao_list as $k=>$v){
			$data[$k]['goods_id']=$v['id'];
			$data[$k]['goods_no']=$v['goods_no'];
			$data[$k]['goods_name']=$v['goods_name'];
			$data[$k]['order_no']=$order_no;
			$data[$k]['add_time']=time();
			$data[$k]['merchant_code']=$param['merchant_code'];
		}
        $insert=$m->addAll($data);
        if(!$insert){
            $return['message']='添加失败';
            $return['status']=0;
            $this->ajaxReturn($return);
        }else{
            $return['message']='添加成功!';
			$return['status']=1; 
			$this->ajaxReturn($return);
        }
	}
	//盘盈核增编辑
	public function checkstocklogSave(){
		$m=M('CheckstockLog',C('DB_PRS'),C('DB_DEPOT'));
        if(!IS_POST) exit;
		$param=I('param.','','trim');
		$status				=$param['status'];
		$id					=$param['id'];
		$data['goods_nums']	=$param['goods_nums'];
		$data['mark']		=$param['mark'];
		$map['id']=$id;
		$m_info=$m->where($map)->find();
		if($m_info['status']==1){
			alert_json(0,'已审核通过不能更改！');
		}
		$res=$m->where($map)->save($data);
        if($res===false){
            $return['message']='编辑失败';
            $return['status']=0;
            $this->ajaxReturn($return);
        }else{
            $return['message']='编辑成功!';
			$return['status']=1; 
			$this->ajaxReturn($return);
        }
	}
	//库内调拨编辑
	public function distributionlogSave(){
		$m=M('DistributionLog',C('DB_PRS'),C('DB_DEPOT'));
        if(!IS_POST) exit;
		$param=I('param.','','trim');
		$status				=$param['status'];
		$id					=$param['id'];
		$data['goods_nums']	=$param['goods_nums'];
		$data['mark']		=$param['mark'];
		$map['id']=$id;
		$m_info=$m->where($map)->find();
		if($m_info['status']==1){
			alert_json(0,'已审核通过不能更改！');
		}
		$res=$m->where($map)->save($data);
        if($res===false){
            $return['message']='编辑失败';
            $return['status']=0;
            $this->ajaxReturn($return);
        }else{
            $return['message']='编辑成功!';
			$return['status']=1; 
			$this->ajaxReturn($return);
        }
	}
	//删除盘盈核增
    public function checkstocklogDel(){
		$m=M('CheckstockLog',C('DB_PRS'),C('DB_DEPOT'));
		$id=I('id');
		$arr=explode(',',$id);
		$map['id']=array('in',$arr);
		$m_list=$m->field('status')->where($map)->select();
		if(deep_in_array(1,$m_list)){
			alert_json(0,'已审核通过不能删除！');
		}
		$return=tableDel('CheckstockLog');
        $this->ajaxReturn($return);
    }
	//删除库内调拨
    public function distributionlogDel(){
		$m=M('DistributionLog',C('DB_PRS'),C('DB_DEPOT'));
		$id=I('id');
		$arr=explode(',',$id);
		$map['id']=array('in',$arr);
		$m_list=$m->field('status')->where($map)->select();
		if(deep_in_array(1,$m_list)){
			alert_json(0,'已审核通过不能删除！');
		}
		$return=tableDel('DistributionLog');
        $this->ajaxReturn($return);
    }
	//清空盘盈核增
    public function checkstocklogClear(){
		$return=tableClear('CheckstockLog');
        $this->ajaxReturn($return);
    }
	//清空库内调拨
    public function distributionlogClear(){
		$return=tableClear('distributionLog');
        $this->ajaxReturn($return);
    }
	//恢复盘盈核增
    public function checkstocklogUndo(){
		$return=tableUndo('CheckstockLog');
        $this->ajaxReturn($return);
    }
	//恢复库内调拨
    public function distributionlogUndo(){
		$return=tableUndo('DistributionLog');
        $this->ajaxReturn($return);
    }
	//盘盈核增审核
    public function examineCheckstocklog(){
		$m=M('CheckstockLog',C('DB_PRS'),C('DB_DEPOT'));
		$ao=M('ArrivalOrder',C('DB_PRS'),C('DB_DEPOT'));
		$id=I('id');
		$value=I('value');
		$arr=explode(',',$id);
		$map['id']=array('in',$arr);
		$m_list=$m->field('status')->where($map)->select();
		if(deep_in_array(1,$m_list)){
			alert_json(0,'已审核通过不能再次审核！');
		}
		//审核通过，更新库存
		if($value==1){
			$trans = M('',C('DB_PRS'), C('DB_DEPOT'));
			$trans->startTrans();   // 开启事务
			$m_list=$m->field('goods_id,cate,goods_nums,status')->where($map)->select();
			foreach($m_list as $k=>$v){
				$map_ao['id']=$v['goods_id'];
				if(empty($v['goods_nums'])){
					alert_json(0,'请先填写数量再审核！');
				}
				$data_ao=array();
				if($v['cate']==1){
					// $data_ao['freez_amount']=array('exp','freez_amount-'.$v['goods_nums']);
					$data_ao['storage_amount']=array('exp','storage_amount+'.$v['goods_nums']);
				}else{
					// $data_ao['freez_amount']=array('exp','freez_amount+'.$v['goods_nums']);
					$data_ao['storage_amount']=array('exp','storage_amount-'.$v['goods_nums']);
				}
				$res_ao=$ao->where($map_ao)->save($data_ao);
				if($res_ao===false){
					$trans->rollback();
					alert_json(0,'失败');
				}
			}
			$trans->commit();
		}
		$return=tableExamine('CheckstockLog');
        $this->ajaxReturn($return);
    }
	//盘盈库内调拨
    public function examineDistributionlog(){
		$m=M('DistributionLog',C('DB_PRS'),C('DB_DEPOT'));
		$ao=M('ArrivalOrder',C('DB_PRS'),C('DB_DEPOT'));
		$id=I('id');
		$value=I('value');
		$arr=explode(',',$id);
		$map['id']=array('in',$arr);
		$m_list=$m->field('status')->where($map)->select();
		if(deep_in_array(1,$m_list)){
			alert_json(0,'已审核通过不能再次审核！');
		}
		//审核通过，更新库存
		if($value==1){
			$trans = M('',C('DB_PRS'), C('DB_DEPOT'));
			$trans->startTrans();   // 开启事务
			$m_list=$m->field('id,goods_id,cate,goods_nums,status')->where($map)->select();
			foreach($m_list as $k=>$v){
				$map_ao['id']=$v['goods_id'];
				if(empty($v['goods_nums'])){
					alert_json(0,'请先填写数量再审核！');
				}
				$data_ao=array();
				if($v['cate']==1){		//独立库存(直接减掉库存)
					$data_ao['storage_amount']=array('exp','storage_amount-'.$v['goods_nums']);
				}else{			//预警库存(不操作)
					
				}
				$res=$m->where(array('id'=>$v['id']))->save(array('storage_amount'=>$v['goods_nums']));
				if($res_ao===false){
					$trans->rollback();
					alert_json(0,'失败');
				}
				$res_ao=$ao->where($map_ao)->save($data_ao);
				if($res_ao===false){
					$trans->rollback();
					alert_json(0,'失败');
				}
			}
			$trans->commit();
		}
		$return=tableExamine('DistributionLog');
        $this->ajaxReturn($return);
    }
	//盘盈核增添加
	public function checkstockscrapAdd(){
		$m=M('CheckstockScrap',C('DB_PRS'),C('DB_DEPOT'));
		$ao=M('ArrivalOrder',C('DB_PRS'),C('DB_DEPOT'));
        if(!IS_POST) exit;
		$param=I('param.','','trim');
		$ids=$param['ids'];
		$ids=explode(',',$ids);
		$map_ao['id']=array('in',$ids);
		$ao_list=$ao->field('id,goods_no,goods_name,order_no')->where($map_ao)->select();
		
		$t_no=serialNumber('CheckstockScrap');//流水号
		$order_no='CL'.date('Ymd',time()).$t_no;//流水单号
		
        foreach($ao_list as $k=>$v){
			$data[$k]['goods_id']=$v['id'];
			$data[$k]['goods_no']=$v['goods_no'];
			$data[$k]['goods_name']=$v['goods_name'];
			$data[$k]['order_no']=$order_no;
			$data[$k]['add_time']=time();
			$data[$k]['cate']=$param['cate'];
		}
        $insert=$m->addAll($data);
        if(!$insert){
            $return['message']='添加失败';
            $return['status']=0;
            $this->ajaxReturn($return);
        }else{
            $return['message']='添加成功!';
			$return['status']=1; 
			$this->ajaxReturn($return);
        }
	}
	//盘盈核增编辑
	public function checkstockscrapSave(){
		$m=M('CheckstockScrap',C('DB_PRS'),C('DB_DEPOT'));
        if(!IS_POST) exit;
		$param=I('param.','','trim');
		$status				=$param['status'];
		$id					=$param['id'];
		$data['goods_nums']	=$param['goods_nums'];
		$data['mark']		=$param['mark'];
		$map['id']=$id;
		$m_info=$m->where($map)->find();
		if($m_info['status']==1){
			alert_json(0,'已审核通过不能更改！');
		}
		$res=$m->where($map)->save($data);
        if($res===false){
            $return['message']='编辑失败';
            $return['status']=0;
            $this->ajaxReturn($return);
        }else{
            $return['message']='编辑成功!';
			$return['status']=1; 
			$this->ajaxReturn($return);
        }
	}
	//删除盘盈核增
    public function checkstockscrapDel(){
		$m=M('CheckstockScrap',C('DB_PRS'),C('DB_DEPOT'));
		$id=I('id');
		$arr=explode(',',$id);
		$map['id']=array('in',$arr);
		$m_list=$m->field('status')->where($map)->select();
		if(deep_in_array(1,$m_list)){
			alert_json(0,'已审核通过不能删除！');
		}
		$return=tableDel('CheckstockScrap');
        $this->ajaxReturn($return);
    }
	//清空盘盈核增
    public function checkstockscrapClear(){
		$return=tableClear('CheckstockScrap');
        $this->ajaxReturn($return);
    }
	//恢复盘盈核增
    public function checkstockscrapUndo(){
		$return=tableUndo('CheckstockScrap');
        $this->ajaxReturn($return);
    }
	//盘盈核增审核
    public function examineCheckstockscrap(){
		$m=M('CheckstockScrap',C('DB_PRS'),C('DB_DEPOT'));
		$ao=M('ArrivalOrder',C('DB_PRS'),C('DB_DEPOT'));
		$id=I('id');
		$value=I('value');
		$arr=explode(',',$id);
		$map['id']=array('in',$arr);
		$m_list=$m->field('status')->where($map)->select();
		if(deep_in_array(1,$m_list)){
			alert_json(0,'已审核通过不能再次审核！');
		}
		//审核通过，更新库存
		if($value==1){
			$trans = M('',C('DB_PRS'), C('DB_DEPOT'));
			$trans->startTrans();   // 开启事务
			$m_list=$m->field('goods_id,cate,goods_nums,status')->where($map)->select();
			foreach($m_list as $k=>$v){
				$map_ao['id']=$v['goods_id'];
				if(empty($v['goods_nums'])){
					alert_json(0,'请先填写数量再审核！');
				}
				if($v['cate']==1){
					// $data_ao['freez_amount']=array('exp','freez_amount-'.$v['goods_nums']);
					$data_ao['storage_amount']=array('exp','storage_amount+'.$v['goods_nums']);
				}else{
					// $data_ao['freez_amount']=array('exp','freez_amount+'.$v['goods_nums']);
					$data_ao['storage_amount']=array('exp','storage_amount-'.$v['goods_nums']);
				}
				$res_ao=$ao->where($map_ao)->save($data_ao);
				if($res_ao===false){
					$trans->rollback();
					alert_json(0,'失败');
				}
			}
			$trans->commit();
		}
		$return=tableExamine('CheckstockScrap');
        $this->ajaxReturn($return);
    }
	//添加耗材入库单
    public function materialAdd(){
		$m=M('PackageMaterialOrder',C('DB_PRS'),C('DB_DEPOT'));
        if(!IS_POST) exit;
        $ck=A('CheckInput'); 
		$data['order_no']=I('order_no');
		$data['section_no']=I('section_no');
		$data['people_make_no']=I('people_make_no');
		$data['people_purchasing_no']=I('people_purchasing_no');
		$data['contacts_company_no']=I('contacts_company_no');
		$data['add_time']=time();
		$data['update_time']=time();
		//重复判断
		$map['order_no']=I('order_no');
		$duplicate=$m->where($map)->find();
		if($duplicate){
			$return['message']='单号已经存在';
            $return['status']=false;
            $this->ajaxReturn($return);
		}
        $insertId=$m->add($data);
        if(!$insertId){
            $return['message']='添加失败';
            $return['status']=false;
            $this->ajaxReturn($return);
        }else{
            $return['message']='添加成功!';
			$return['status']=true; 
			$this->ajaxReturn($return);
        }
    }
	//添加耗材账册信息
    public function packagematerialbooksAdd(){
		$m=M('PackageMaterialBooks',C('DB_PRS'),C('DB_DEPOT'));
        if(!IS_POST) exit;
        $ck=A('CheckInput'); 
		$data['name']=I('name');
		$data['pn']=I('pn');
		$length=I('length');
		$width=I('width');
		$height=I('height');
		$data['l_w_h']=$length."_".$width."_".$height;
		$data['add_time']=time();

		//重复判断
		$map['pn']=I('pn');
		$duplicate=$m->where($map)->find();
		if($duplicate){
			$return['message']='条形码已经存在';
            $return['status']=false;
            $this->ajaxReturn($return);
		}
        $insertId=$m->add($data);
        if(!$insertId){
            $return['message']='添加失败';
            $return['status']=false;
            $this->ajaxReturn($return);
        }else{
            $return['message']='添加成功!';
			$return['status']=true; 
			$this->ajaxReturn($return);
        }
    }
	//编辑耗材账册信息
    public function editpackagematerialbooks(){
		$m=M('PackageMaterialBooks',C('DB_PRS'),C('DB_DEPOT'));
        if(!IS_POST) exit;
        $ck=A('CheckInput'); 
		$data['name']=I('name');
		// $data['pn']=I('pn');
		$length=I('length');
		$width=I('width');
		$height=I('height');
		$data['l_w_h']=$length."_".$width."_".$height;
		// $data['examine_time']=time();

		//重复判断
		$map['id']=I('id');

        $status=$m->where($map)->save($data);
        if($status===false){
            $return['message']='修改失败';
            $return['status']=false;
            $this->ajaxReturn($return);
        }else{
            $return['message']='修改成功!';
			$return['status']=true; 
			$this->ajaxReturn($return);
        }
    }
	//添加库位
    public function storageAdd(){
		$m=M('Storage',C('DB_PRS'),C('DB_DEPOT'));
        if(!IS_POST) exit;
        $ck=A('CheckInput'); 
		$data['storage_code']=I('storage_code');
		$data['name']=I('name');
		$data['state']=I('state');
		$data['storage_type']=I('storage_type');
		$data['storage_ltype']=I('storage_ltype');
		$data['max_volume']=I('max_volume');
		$data['uses']=I('uses');
		$data['remarks']=I('remarks');
		$data['add_time']=time();
		$data['update_time']=time();
		//重复判断
		$map['storage_code']=I('storage_code');
		$duplicate=$m->where($map)->find();
		if($duplicate){
			$return['message']='库位编号已经存在';
            $return['status']=false;
            $this->ajaxReturn($return);
		}
        $insertId=$m->add($data);
        if(!$insertId){
            $return['message']='添加失败';
            $return['status']=false;
            $this->ajaxReturn($return);
        }else{
            $return['message']='添加成功!';
			$return['status']=true; 
			$this->ajaxReturn($return);
        }
    }
	//添加调拨单
    public function moveDepotAdd(){
		$m=M('MoveDepot',C('DB_PRS'),C('DB_DEPOT'));
        if(!IS_POST) exit;
        $ck=A('CheckInput'); 
		$data['order_no']=I('order_no');
		$data['section_no']=I('section_no');
		$data['people_make_no']=I('people_make_no');
		$data['people_purchasing_no']=I('people_purchasing_no');
		$data['contacts_company_no']=I('contacts_company_no');
		$data['depot_code']=I('depot_code');
		$data['add_time']=time();
		$data['update_time']=time();
		//重复判断
		$map['order_no']=I('order_no');
		$duplicate=$m->where($map)->find();
		if($duplicate){
			$return['message']='单号已经存在';
            $return['status']=false;
            $this->ajaxReturn($return);
		}
        $insertId=$m->add($data);
        if(!$insertId){
            $return['message']='添加失败';
            $return['status']=false;
            $this->ajaxReturn($return);
        }else{
            $return['message']='添加成功!';
			$return['status']=true; 
			$this->ajaxReturn($return);
        }
    }
	//添加其它入库单
    public function otherInboundAdd(){
		$m=M('OtherInbound',C('DB_PRS'),C('DB_DEPOT'));
        if(!IS_POST) exit;
        $ck=A('CheckInput'); 
		$data['order_no']=I('order_no');
		$data['batch_no']=I('batch_no');
		$data['section_no']=I('section_no');
		$data['people_make_no']=I('people_make_no');
		$data['people_purchasing_no']=I('people_purchasing_no');
		$data['contacts_company_no']=I('contacts_company_no');
		$data['bound_cate_code']=I('bound_cate_code');
		$data['add_time']=time();
		$data['update_time']=time();
		//重复判断
		$map['order_no']=I('order_no');
		$duplicate=$m->where($map)->find();
		if($duplicate){
			$return['message']='单号已经存在';
            $return['status']=false;
            $this->ajaxReturn($return);
		}
        $insertId=$m->add($data);
        if(!$insertId){
            $return['message']='添加失败';
            $return['status']=false;
            $this->ajaxReturn($return);
        }else{
            $return['message']='添加成功!';
			$return['status']=true; 
			$this->ajaxReturn($return);
        }
    }
	//添加其它出库单
    public function otherOutboundAdd(){
		$m=M('OtherOutbound',C('DB_PRS'),C('DB_DEPOT'));
        if(!IS_POST) exit;
        $ck=A('CheckInput'); 
		$data['order_no']=I('order_no');
		$data['section_no']=I('section_no');
		$data['people_make_no']=I('people_make_no');
		$data['people_purchasing_no']=I('people_purchasing_no');
		$data['contacts_company_no']=I('contacts_company_no');
		$data['bound_cate_code']=I('bound_cate_code');
		$data['add_time']=time();
		$data['update_time']=time();
		//重复判断
		$map['order_no']=I('order_no');
		$duplicate=$m->where($map)->find();
		if($duplicate){
			$return['message']='单号已经存在';
            $return['status']=false;
            $this->ajaxReturn($return);
		}
        $insertId=$m->add($data);
        if(!$insertId){
            $return['message']='添加失败';
            $return['status']=false;
            $this->ajaxReturn($return);
        }else{
            $return['message']='添加成功!';
			$return['status']=true; 
			$this->ajaxReturn($return);
        }
    }
	//更新耗材入库单
    public function materialSave(){
		$m=M('PackageMaterialOrder',C('DB_PRS'),C('DB_DEPOT'));
        if(!IS_POST) exit; 
        $ck=A('CheckInput');
		$param=I('param.','','trim');
		$data['section_no']=$param['section_no'];
		$data['people_make_no']=$param['people_make_no'];
		$data['people_purchasing_no']=$param['people_purchasing_no'];
		$data['contacts_company_no']=$param['contacts_company_no'];
		$data['update_time']=time();
        $map['id']=$ck->in('id','id','intval','',1,0);
		
        $result=$m->where($map)->save($data);
        if(false===$result){
            $return['message']='更新信息失败';
            $return['status']=false;
            $this->ajaxReturn($return);
        }else{
            $return['message']='更新信息成功!';
			$return['status']=true; 
			$this->ajaxReturn($return);
        }
    }
	//设为默认耗材
	public function setdefault_packagematerial(){
		$m=M('PackageMaterial',C('DB_PRS'),C('DB_DEPOT'));
		$id=I('id');
		$value=I('value');
		if($value==1){
			$count=$m->where(array('default'=>1))->count();
			if($count>0){
				$return['message']='已存在默认耗材，请先取消';
				$return['status']=false;
				$this->ajaxReturn($return);
			}
		}
		$res=$m->where(array('id'=>$id))->save(array('default'=>$value));
		if($res===false){
			$return['message']='设置失败';
            $return['status']=false;
            $this->ajaxReturn($return);
		}else{
			$return['message']='设置成功';
            $return['status']=true;
            $this->ajaxReturn($return);
		}
	}
	//更新库位信息
    public function storageSave(){
		$m=M('Storage',C('DB_PRS'),C('DB_DEPOT'));
        if(!IS_POST) exit; 
        $ck=A('CheckInput');
		$param=I('param.','','trim');
		$data['name']=$param['name'];
		$data['state']=$param['state'];
		$data['storage_type']=$param['storage_type'];
		$data['storage_ltype']=$param['storage_ltype'];
		$data['max_volume']=$param['max_volume'];
		$data['uses']=$param['uses'];
		$data['sort']=$param['sort'];
		$data['remarks']=$param['remarks'];
		$data['update_time']=time();
        $map['id']=$ck->in('id','id','intval','',1,0);
		
        $result=$m->where($map)->save($data);
        if(false===$result){
            $return['message']='更新信息失败';
            $return['status']=false;
            $this->ajaxReturn($return);
        }else{
            $return['message']='更新信息成功!';
			$return['status']=true; 
			$this->ajaxReturn($return);
        }
    }
	//更新其它入库单
    public function otherInboundSave(){
		$m=M('OtherInbound',C('DB_PRS'),C('DB_DEPOT'));
        if(!IS_POST) exit; 
        $ck=A('CheckInput');
		$param=I('param.','','trim');
		$data['section_no']=$param['section_no'];
		$data['people_make_no']=$param['people_make_no'];
		$data['people_purchasing_no']=$param['people_purchasing_no'];
		$data['contacts_company_no']=$param['contacts_company_no'];
		$data['bound_cate_code']=$param['bound_cate_code'];
		$data['update_time']=time();
        $map['id']=$ck->in('id','id','intval','',1,0);
		
        $result=$m->where($map)->save($data);
        if(false===$result){
            $return['message']='更新信息失败';
            $return['status']=false;
            $this->ajaxReturn($return);
        }else{
            $return['message']='更新信息成功!';
			$return['status']=true; 
			$this->ajaxReturn($return);
        }
    }
	//更新调拨单
    public function moveDepotSave(){
		$m=M('MoveDepot',C('DB_PRS'),C('DB_DEPOT'));
        if(!IS_POST) exit; 
        $ck=A('CheckInput');
		$data['section_no']=I('section_no');
		$data['people_make_no']=I('people_make_no');
		$data['people_purchasing_no']=I('people_purchasing_no');
		$data['contacts_company_no']=I('contacts_company_no');
		$data['depot_code']=I('depot_code');
		$data['update_time']=time();
        $map['id']=$ck->in('id','id','intval','',1,0);
		
        $result=$m->where($map)->save($data);
		
        if(false===$result){
            $return['message']='更新信息失败';
            $return['status']=false;
            $this->ajaxReturn($return);
        }else{
            $return['message']='更新信息成功!';
			$return['status']=true; 
			$this->ajaxReturn($return);
        }
    }
	//更新其它出库单
    public function otherOutboundSave(){
		$m=M('OtherOutbound',C('DB_PRS'),C('DB_DEPOT'));
        if(!IS_POST) exit; 
        $ck=A('CheckInput');
		$data['section_no']=I('section_no');
		$data['people_make_no']=I('people_make_no');
		$data['people_purchasing_no']=I('people_purchasing_no');
		$data['contacts_company_no']=I('contacts_company_no');
		$data['bound_cate_code']=I('bound_cate_code');
		$data['update_time']=time();
        $map['id']=$ck->in('id','id','intval','',1,0);
		
        $result=$m->where($map)->save($data);
		
        if(false===$result){
            $return['message']='更新信息失败';
            $return['status']=false;
            $this->ajaxReturn($return);
        }else{
            $return['message']='更新信息成功!';
			$return['status']=true; 
			$this->ajaxReturn($return);
        }
    }
	//审核耗材入库单
    public function examinematerial(){
		//审核判断详情为空或值为0
		$m=M('PackageMaterialOrderinfo',C('DB_PRS'),C('DB_DEPOT'));
		$id=I('id');
		$arr=explode(',',$id);
		foreach($arr as $k=>$v){
			$map['order_id']=$v;
			$info=$m->where($map)->select();
			if(!$info){
				$return['message']='没有详情信息，请添加详情!';
				$return['status']=false; 
				$this->ajaxReturn($return);
				die;
			}else{
				foreach($info as $key=>$value){
					if(empty($value['nums']) or $value['nums']==0){
						$return['message']='数量不能为空或0!';
						$return['status']=false; 
						$this->ajaxReturn($return);
						die;
					}
				}
			}
		}
		$return=tableExamine('PackageMaterialOrder');
        $this->ajaxReturn($return);
    }
	//审核调拨单
    public function examinemoveDepot(){
		//审核判断详情为空或值为0
		$m=M('MoveDepotInfo',C('DB_PRS'),C('DB_DEPOT'));
		$mt=M('MoveDepot',C('DB_PRS'),C('DB_DEPOT'));
		$ao=M('ArrivalOrder',C('DB_PRS'),C('DB_DEPOT'));
		$value_s=I('value');
		$id=I('id');
		$arr=explode(',',$id);
		foreach($arr as $k=>$v){
			$map['order_id']=$v;
			$map_order['id']=$v;
			$info_order=$mt->where($map_order)->find();
			if($info_order['status']==1){
				$return['message']='已审核通过，不能再次审核!';
				$return['status']=false; 
				$this->ajaxReturn($return);
			}
			$info=$m->where($map)->select();
			if(!$info){
				$return['message']='没有详情信息，请添加详情!';
				$return['status']=false; 
				$this->ajaxReturn($return);
				
			}else{
				foreach($info as $key=>$value){
					if(empty($value['nums']) or $value['nums']==0){
						$return['message']='数量不能为空或0!';
						$return['status']=false; 
						$this->ajaxReturn($return);
					}else{
						if($value_s==1){//通过则冻结库存
							//冻结要其它出库数量
							$data_ao['freez_amount']=array('exp','freez_amount+'.$value['nums']);
							$data_ao['storage_amount']=array('exp','storage_amount-'.$value['nums']);
							$ao->where(array('id'=>$value['goods_id']))->save($data_ao);//更新库存并冻结要出库数量
							// echo $ao->getlastsql();
							// die;
						}
					}
				}
			}
		}
		$return=tableExamine('MoveDepot');
        $this->ajaxReturn($return);
    }
	//出库调拨单
    public function outbound_moveDepot(){
		$so=M('StorageOutbound',C('DB_PRS'),C('DB_DEPOT'));
		$mm=M('MoveDepot',C('DB_PRS'),C('DB_DEPOT'));
		$mmi=M('MoveDepotInfo',C('DB_PRS'),C('DB_DEPOT'));
		$ao=M('ArrivalOrder',C('DB_PRS'),C('DB_DEPOT'));
		$id=I('id');
		$value=I('value');
		$arr=explode(',',$id);
		foreach($arr as $k=>$v){
			$info=$mm->where(array('id'=>$v))->find();
			if($info['status']!=1){
				$return['info']='未审核通过，不能出库!';
				$return['status']=false; 
				$this->ajaxReturn($return);
				die;
			}
			if($info['out_status']==1){
				$return['info']='已出库，不能再次出库!';
				$return['status']=false; 
				$this->ajaxReturn($return);
				die;
			}
			$order_list=$mmi->where(array('order_id'=>$v))->select();
			foreach($order_list as $ks=>$vs){
				$map_ao['id']=$vs['goods_id'];
				$data_ao['freez_amount']=array('exp','freez_amount-'.$vs['nums']);//释放冻结数量
				$ao_info=$ao->where($map_ao)->find();//取得当前批次详情信息
				$ao->where($map_ao)->save($data_ao);//更新
				// echo $ao->getlastsql();
				// die;
				$so->add(array('outbound_order_id'=>$info['id'],'inbound_order_id'=>$vs['goods_id'],'nums'=>$vs['nums'],'add_time'=>time(),'out_cate'=>4));//更新出库记录out_cate:(出库类型1:订单出库2:异常出库3:销售单出库数量4:调拨出库5:领料出库6:其它出库)
			}
			$mm->where(array('id'=>$v))->setField('out_status',1);//更新出库状态
		}
		$return['info']='成功出库!';
		$return['status']=true;
		$this->ajaxReturn($return);
    }

	//审核其它入库单
    public function examineOtherInbound(){
		//审核判断详情为空或值为0
		$m=M('OtherInboundInfo',C('DB_PRS'),C('DB_DEPOT'));
		$id=I('id');
		$arr=explode(',',$id);
		foreach($arr as $k=>$v){
			$map['order_id']=$v;
			$info=$m->where($map)->select();
			if(!$info){
				$return['message']='没有详情信息，请添加详情!';
				$return['status']=false; 
				$this->ajaxReturn($return);
				
			}else{
				foreach($info as $key=>$value){
					if(empty($value['unit_count']) or $value['unit_count']==0){
						$return['message']='数量不能为空或0!';
						$return['status']=false; 
						$this->ajaxReturn($return);
					}
				}
			}
		}
		$return=tableExamine('OtherInbound');
        $this->ajaxReturn($return);
    }
	//推送其它入库单
    public function pushOtherInbound(){
		//推送到货通知单
		$m=M('OtherInbound',C('DB_PRS'),C('DB_DEPOT'));
		$n=M('OtherInboundInfo',C('DB_PRS'),C('DB_DEPOT'));
		$b=M('Books',C('DB_PRS'),C('DB_DEPOT'));
		// $c=M('BooksCate',C('DB_PRS'),C('DB_DEPOT'));
		$a=M('ArrivalOrder',C('DB_PRS'),C('DB_DEPOT'));
		$ap=M('ArrivalOrderPlan',C('DB_PRS'),C('DB_DEPOT'));
		$id=I('id');
		
		$ids=explode(',',$id);
		$map['id']=array('in',$ids);
		$list_order=$m->where($map)->select();
		
		foreach($list_order as $key=>$value){
			if(empty($value['examine_time'])){
				$return['status']=0;
				$return['info']="未审核不能推送";
				$this->ajaxReturn($return);
			}
			if(!$value['push_status']){//未推送
				$info_order_list=$n->where(array('order_id'=>$value['id']))->select();
				// $data_plan=[];
				// $data_plan=
				// $res_plan=$ap->add($data_plan);
				// echo $n->getlastsql();
				foreach($info_order_list as $k=>$v){
					$goods=$b->where(array('id'=>$v['books_id']))->find();
					// $bookscate=$c->where(array('code'=>$goods['manua_id']))->find();
					// echo $b->getlastsql();
					// echo $c->getlastsql();
					// die;
					//推到到货通知表
					$list['order_no']='OI'.$value['order_no'];
					$list['batch_no']='OI'.$value['order_no'];
					$list['old_purchasing_no']=$value['order_no'];
					$list['manual_id']=$goods['manua_id'];
					// $list['erp_code']=$bookscate['erp_code'];
					$list['goods_spec']=$goods['goods_spec'];
					$list['goods_no']=$goods['goods_no'];
					$list['goods_name']=$goods['goods_name'];
					$list['amount']=$v['unit_count'];//计划入库数量
					$list['receive_amount']=$v['unit_count'];//实际入库数量
					$list['storage_amount']=$v['unit_count'];//库存数量
					$list['declare_amount']=$v['unit_count'];//申报数量
					$list['bound_cate_code']='OI';
					$list['inbound_status']=1;
					$list['status']=1;
					// $list['cloud_depot_status']=$v['cloud_depot_status'];//是否云仓商品
					// $list['cloud_num']=$v['cloud_num'];//云仓商品数量
					// $list['cloud_unit']=$v['cloud_unit'];//云仓商品单位
					$list['expected_time']=time();//预计到货时间
					$list['add_time']=time();
					if(empty($v['price'])){
						$list['price']=$a->where(array('goods_no'=>$goods['goods_no']))->getField('price');
					}else{
						$list['price']=$v['price'];
					}
					$list['conforming_p']=$v['conforming_p'];
					$list['nonconforming_p']=$v['nonconforming_p'];
					$list['scrap_one']=$v['scrap_one'];
					$list['unit1']=$goods['unit1'];
					$list['currency']=$goods['current_type'];
					$list['country_code']=$goods['source'];
					
					$list['bar_code']=$goods['source_no'];
					$list['source_no']=$goods['source_no'];
					
					$status=$a->add($list);//到货通知单添加
					// echo $a->getlastsql();
					// die;
					if(!$status){
						$return['status']=0;
						$return['info']="添加失败";
						$this->ajaxReturn($return);
					}else{
						//更新商品库存信息
						$g=M('Goods',C('DB_PRS'),C('DB_DEPOT'));
						$receive_amount=$v['unit_count'];//实收数量
						$conforming_p=$v['unit_count'];//合格品数量
						$nonconforming_p=0;//残次品数量
						$scrap_one=0;//报废品数量
						//where唯一条件:商品编码相同，账册编码相同
						$where['goods_no']=$goods['goods_no'];
						$where['manua_id']=$goods['manua_id'];
						
						$res=$g->where($where)->find();
						// echo $g->getlastsql();
						// die;
						$data=[];
						if($res){//库存中已经有此商品，则更新库存数据
							$data['amount']=array('exp',"amount+$receive_amount");
							$data['conforming_p']=array('exp',"conforming_p+$conforming_p");
							$data['nonconforming_p']=array('exp',"nonconforming_p+$nonconforming_p");
							$data['scrap_one']=array('exp',"scrap_one+$scrap_one");
							//价格(平均价)=总价/总数量
							$data['goods_price']=($res['goods_price']*$res['amount']+$v['unit_count']*$v['price'])/($res['amount']+$v['unit_count']);
							$ress=$g->where($where)->save($data);
							if($ress===false){
								$this->error($goods['goods_name'].'| 入库失败！');
							}else{
								$a->where(array('goods_no'=>$goods['goods_no'],'manual_id'=>$goods['manua_id']))->setField('bar_code',$res['bar_code']);//更新条形码
							}
						}else{//库存中没有此商品,则添加此商品数据入库存
							$data['source_no']=$goods['source_no'];
							$data['goods_name']=$goods['goods_name'];
							$data['goods_no']=$goods['goods_no'];
							$data['bar_code']=$goods['source_no'];
							$data['amount']=$receive_amount;
							$data['conforming_p']=$v['unit_count'];
							$data['goods_price']=$v['price'];
							$data['declare_unit']=$goods['unit1'];
							$data['unit1']=$goods['unit1'];
							$data['unit2']=$goods['unit2'];
							$data['nonconforming_p']=0;
							$data['scrap_one']=0;
							$data['manua_id']=$goods['manua_id'];
							$data['goods_spec']=$goods['goods_spec'];
							// $data['arrival_time'] = time();
							$resss=$g->add($data);
							// echo $g->_sql();
							if(!$resss){
								// echo $g->_sql();
								$this->error($goods['goods_name'].'| 入库失败！');
							}
						}
					}
				}
				M('OtherInbound',C('DB_PRS'),C('DB_DEPOT'))->where(array('id'=>$value['id']))->setField('push_status',1);
				
				
				//按企业编码扣费--begin
				out_money(19,1,16);//19：退货商品清点
				out_money(20,1,16);//20：客户退货验收入库
				out_money(21,1,16);//21：客户退货上架
				//扣费--end
				$return['status']=1;
				$return['info']="入库成功！";
				$this->ajaxReturn($return);
			}else{
				$return['status']=0;
				$return['info']="已入库过了";
				$this->ajaxReturn($return);
			}
		}
		$this->ajaxReturn($return);
    }
	
	//删除耗材账册
    public function packagematerialbooksDel(){
		$return=tableDel('PackageMaterialBooks');
        $this->ajaxReturn($return);
    }
	public function packagematerialbooksUndo(){
		$return=tableUndo('PackageMaterialBooks');
        $this->ajaxReturn($return);
	}
	public function packagematerialbooksClear(){
		$return=tableClear('PackageMaterialBooks');
        $this->ajaxReturn($return);
	}
	//删除耗材入库单
    public function materialDel(){
		$return=tableDel('PackageMaterialOrder');
        $this->ajaxReturn($return);
    }
	//删除库位
    public function storageDel(){
		$return=tableDel('Storage');
        $this->ajaxReturn($return);
    }
	//删除调拨单
    public function moveDepotDel(){
		$return=tableDel('MoveDepot');
        $this->ajaxReturn($return);
    }
	//删除其它入库单
    public function otherInboundDel(){
		$return=tableDel('OtherInbound');
        $this->ajaxReturn($return);
    }
	//清空耗材入库单回收站
    public function materialClear(){
		$return=tableClear('PackageMaterialOrder');
        $this->ajaxReturn($return);
    }
	//清空库位回收站
    public function storageClear(){
		$return=tableClear('Storage');
        $this->ajaxReturn($return);
    }
	//清空调拨单回收站
    public function moveDepotClear(){
		$return=tableClear('MoveDepot');
        $this->ajaxReturn($return);
    }
	//清空其它入库单回收站
    public function otherInboundClear(){
		$return=tableClear('OtherInbound');
        $this->ajaxReturn($return);
    }
	//恢复耗材入库单回收站回收站数据
    public function materialUndo(){
		$return=tableUndo('PackageMaterialOrder');
        $this->ajaxReturn($return);
    }
	//恢复调拨单回收站数据
    public function moveDepotUndo(){
		$return=tableUndo('MoveDepot');
        $this->ajaxReturn($return);
    }
	//恢复其它入库单回收站数据
    public function otherInboundUndo(){
		$return=tableUndo('OtherInbound');
        $this->ajaxReturn($return);
    }
	//审核其它出库单
    public function examineOtherOutbound(){
		//审核判断详情为空或值为0
		$trans = M('',C('DB_PRS'), C('DB_DEPOT'));
		$trans->startTrans();   // 开启事务
		$m=M('OtherOutboundInfo',C('DB_PRS'),C('DB_DEPOT'));
		$mt=M('OtherOutbound',C('DB_PRS'),C('DB_DEPOT'));
		$ao=M('ArrivalOrder',C('DB_PRS'),C('DB_DEPOT'));
		$value_s=I('value');
		$id=I('id');
		$arr=explode(',',$id);
		foreach($arr as $k=>$v){
			$map['order_id']=$v;
			$map_order['id']=$v;
			$info_order=$mt->where($map_order)->find();
			if($info_order['status']==1){
				$return['message']='已审核通过，不能再次审核!';
				$return['status']=false; 
				$this->ajaxReturn($return);
			}
			$info=$m->where($map)->select();
			if(!$info){
				$return['message']='没有详情信息，请添加详情!';
				$return['status']=false; 
				$this->ajaxReturn($return);
				
			}else{
				foreach($info as $key=>$value){

					if(empty($value['unit_count']) or $value['unit_count']==0){
						$return['message']='数量不能为空或0!';
						$return['status']=false; 
						$this->ajaxReturn($return);
					}else{
						if($value_s==1){
							//冻结要其它出库数量
							$data_ao['freez_amount']=array('exp','freez_amount+'.$value['unit_count']);
							$data_ao['storage_amount']=array('exp','storage_amount-'.$value['unit_count']);
							//残次品出库（废品出库）
							if($value['outbound_cate_no']=='J'){
                                $data_ao['scrap_one']=array('exp','scrap_one-'.$value['unit_count']);
                            }
							$ao_info=$ao->where(array('id'=>$value['goods_id']))->find();
							
							if($ao_info['storage_amount']<$value['unit_count']){
								$trans->rollback();
								$return['message']=$ao_info['goods_no'].'的批次库存数量不足，请检查输入数量!';
								$return['status']=false; 
								$this->ajaxReturn($return);
							}
							$res=$ao->where(array('id'=>$value['goods_id']))->save($data_ao);//更新库存并冻结要出库数量
							if($res===false){
								$trans->rollback();
								$return['message']='数量不能为空或0!';
								$return['status']=false; 
								$this->ajaxReturn($return);
							}
						}
					}
				}
			}
		}
		
		$return=tableExamine('OtherOutbound');
		$trans->commit();
        $this->ajaxReturn($return);
    }
	//推送其它出库单
    public function pushOtherOutbound(){
		//推送到货通知单
		$m=M('OtherOutbound',C('DB_PRS'),C('DB_DEPOT'));
		$n=M('OtherOutboundInfo',C('DB_PRS'),C('DB_DEPOT'));
		$b=M('Goods',C('DB_PRS'),C('DB_DEPOT'));
		$c=M('BooksCate',C('DB_PRS'),C('DB_DEPOT'));
		$p=M('Pushorder',C('DB_PRS'),C('DB_DEPOT'));
		$g=M('Pushordergoods',C('DB_PRS'),C('DB_DEPOT'));
		
		$id=I('id');

		$ids=explode(',',$id);
		$map['id']=array('in',$ids);
		$list_order=$m->where($map)->select();
		
		foreach($list_order as $key=>$value){
			if(empty($value['examine_time'])){
				$return['status']=0;
				$return['info']="未审核不能推送";
				$this->ajaxReturn($return);
			}
			if(!$value['push_status']){//未推送
				$pushorder['order_no']=$value['order_no'];
				$pushorder['user_name']='其它出库';
				$res=$p->add($pushorder);
				if($res){
					$info_order_list=$n->where(array('order_id'=>$value['id']))->select();
					// echo $n->getlastsql();
					foreach($info_order_list as $k=>$v){
						$goods=$b->where(array('id'=>$v['goods_id']))->find();
						$bookscate=$c->where(array('code'=>$goods['manua_id']))->find();
						// echo $b->getlastsql();
						// echo $c->getlastsql();
						// die;
						//推到到货通知表
						$list['order_id']=$res;
						$list['goods_no']=$goods['goods_no'];
						$list['goods_name']=$goods['goods_name'];
						$list['goods_nums']=$v['unit_count'];
						$list['goods_price']=$goods['goods_price'];//价格
						$list['addtime']=time();
						$status=$g->add($list);//到货通知单添加
						// echo $g->getlastsql();
						// die;
						if(!$status){
							$return['status']=0;
							$return['info']="推送订单详情信息失败";
							$this->ajaxReturn($return);
						}
					}
					$m->where(array('id'=>$value['id']))->setField('push_status',1);
					$return['status']=1;
					$return['info']="推送成功";
					$this->ajaxReturn($return);
				}else{
					$return['status']=0;
					$return['info']="推送订单信息失败";
					$this->ajaxReturn($return);
				}
				
			}else{
				$return['status']=0;
				$return['info']="已经推送";
				$this->ajaxReturn($return);
			}
		}
		
		
		$this->ajaxReturn($return);
    }
	/* //推送仓库入库单(仓库调拨)
    public function pushDepotInbound(){
		//推送到货通知单
		$m=M('MoveDepot',C('DB_PRS'),C('DB_DEPOT'));//加载默认配置
		$n=M('MoveDepotInfo',C('DB_PRS'),C('DB_DEPOT'));
		$b=M('Goods',C('DB_PRS'),C('DB_DEPOT'));
		$c=M('BooksCate',C('DB_PRS'),C('DB_DEPOT'));
		$a=M('ArrivalOrder',C('DB_PRS'),C('DB_DEPOT'));
		$ap=M('ArrivalOrderPlan',C('DB_PRS'),C('DB_DEPOT'));
		
		$id=I('id');
		$ids=explode(',',$id);
		$map['id']=array('in',$ids);
		$list_order=$m->where($map)->select();

		foreach($list_order as $key=>$value){
			$w=M('Warehouse');
			$warehouse_info=$w->where(array('warehouse_code'=>$value['depot_code']))->find();
			// echo $w->getlastsql();
			if($warehouse_info){  //天众数据库库配置
				!empty($warehouse_info['db_ip'])?$db_ip=$warehouse_info['db_ip']:$db_ip=C('DEPOT_DB_HOST');
				!empty($warehouse_info['db_name'])?$db_name=$warehouse_info['db_name']:$db_name=C('DEPOT_DB_NAME');
				!empty($warehouse_info['db_username'])?$db_username=$warehouse_info['db_username']:$db_username=C('DEPOT_DB_USER');
				!empty($warehouse_info['db_password'])?$db_password=$warehouse_info['db_password']:$db_password=C('DEPOT_DB_PWD');
				!empty($warehouse_info['db_port'])?$db_port=$warehouse_info['db_port']:$db_port=C('DEPOT_DB_PORT');
				!empty($warehouse_info['db_pre'])?$db_pre=$warehouse_info['db_pre']:$db_pre=C('DEPOT_DB_PREFIX');
				
				//目标仓库数据库配置
				C('DB_DEPOTS.DB_TYPE','mysql');
				C('DB_DEPOTS.DB_HOST',$db_ip);
				C('DB_DEPOTS.DB_NAME',$db_name);
				C('DB_DEPOTS.DB_USER',$db_username);
				C('DB_DEPOTS.DB_PWD',$db_password);
				C('DB_DEPOTS.DB_PORT',$db_port);
				C('DB_PRSSS',$db_pre);
				C('DB_DEPOTS.DB_CHARSET','utf8');
			}
			
			$a_s=M('ArrivalOrder',C('DB_PRSSS'),C('DB_DEPOTS'));
			$ap_s=M('ArrivalOrderPlan',C('DB_PRS'),C('DB_DEPOT'));
		
			if(empty($value['examine_time'])){
				$return['status']=0;
				$return['info']="未审核不能推送";
				$this->ajaxReturn($return);
			}
			if(!$value['push_inbound_status']){//未推送
				$info_order_list=$n->where(array('order_id'=>$value['id']))->select();
				// echo $n->getlastsql();
				foreach($info_order_list as $k=>$v){
					$goods=$b->where(array('id'=>$v['goods_id']))->find();
					$bookscate=$c->where(array('code'=>$goods['manua_id']))->find();
					// echo $b->getlastsql();
					// echo $c->getlastsql();
					
					//推到到货通知表
					$list['order_no']=$value['order_no'];
					$list['old_purchasing_no']=$value['order_no'];
					$list['manual_id']=$goods['manua_id'];
					$list['erp_code']=$bookscate['erp_code'];
					$list['goods_no']=$goods['goods_no'];
					$list['goods_name']=$goods['goods_name'];
					$list['amount']=$v['nums'];
					$list['bound_cate_code']='OI';
					$list['cloud_depot_status']=$v['cloud_depot_status'];//是否云仓商品
					$list['cloud_num']=$v['cloud_num'];//云仓商品数量
					$list['cloud_unit']=$v['cloud_unit'];//云仓商品单位
					$list['expected_time']=$v['expected_time'];//预计到货时间
					$list['add_time']=time();
					$status=$a_s->add($list);//到货通知单添加
		 
		 
					// dump($a_s);
					// die;
					if(!$status){
						$return['status']=0;
						$return['info']="添加失败";
						$this->ajaxReturn($return);
					}
				}
				M('MoveDepot',C('DB_PRS'),C('DB_DEPOT'))->where(array('id'=>$value['id']))->setField('push_inbound_status',1);
				$return['status']=1;
				$return['info']="推送成功！";
				$this->ajaxReturn($return);
			}else{
				$return['status']=0;
				$return['info']="已推送过了";
				$this->ajaxReturn($return);
			}
			
		}
		
		$this->ajaxReturn($return);
    } */

	//推送仓库其它入库单(仓库调拨)
    public function pushDepotInbound(){
		//推送到货通知单
		$m=M('MoveDepot',C('DB_PRS'),C('DB_DEPOT'));//加载默认配置
		$n=M('MoveDepotInfo',C('DB_PRS'),C('DB_DEPOT'));
		$b=M('ArrivalOrder',C('DB_PRS'),C('DB_DEPOT'));
		$c=M('BooksCate',C('DB_PRS'),C('DB_DEPOT'));
		
		$id=I('id');
		$ids=explode(',',$id);
		$map['id']=array('in',$ids);
		$list_order=$m->where($map)->select();

		foreach($list_order as $key=>$value){
			$w=M('Warehouse');
			$warehouse_info=$w->where(array('warehouse_code'=>$value['depot_code']))->find();
			// echo $w->getlastsql();
			if($warehouse_info){  //天众数据库库配置
				!empty($warehouse_info['db_ip'])?$db_ip=$warehouse_info['db_ip']:$db_ip=C('DEPOT_DB_HOST');
				!empty($warehouse_info['db_name'])?$db_name=$warehouse_info['db_name']:$db_name=C('DEPOT_DB_NAME');
				!empty($warehouse_info['db_username'])?$db_username=$warehouse_info['db_username']:$db_username=C('DEPOT_DB_USER');
				!empty($warehouse_info['db_password'])?$db_password=$warehouse_info['db_password']:$db_password=C('DEPOT_DB_PWD');
				!empty($warehouse_info['db_port'])?$db_port=$warehouse_info['db_port']:$db_port=C('DEPOT_DB_PORT');
				!empty($warehouse_info['db_pre'])?$db_pre=$warehouse_info['db_pre']:$db_pre=C('DEPOT_DB_PREFIX');
				
				//目标仓库数据库配置
				C('DB_DEPOTS.DB_TYPE','mysql');
				C('DB_DEPOTS.DB_HOST',$db_ip);
				C('DB_DEPOTS.DB_NAME',$db_name);
				C('DB_DEPOTS.DB_USER',$db_username);
				C('DB_DEPOTS.DB_PWD',$db_password);
				C('DB_DEPOTS.DB_PORT',$db_port);
				C('DB_PRSSS',$db_pre);
				C('DB_DEPOTS.DB_CHARSET','utf8');
			}
			
			$a_s=M('OtherInboundInfo',C('DB_PRSSS'),C('DB_DEPOTS'));
			$ap_s=M('OtherInbound',C('DB_PRS'),C('DB_DEPOTS'));
			$b_s=M('ArrivalOrder',C('DB_PRS'),C('DB_DEPOTS'));
			$d_s=M('Books',C('DB_PRS'),C('DB_DEPOTS'));
			
			
			if(empty($value['examine_time'])){
				$return['status']=0;
				$return['info']="未审核不能推送";
				$this->ajaxReturn($return);
			}
			if(!$value['push_inbound_status']){//未推送
				$info_order_list=$n->where(array('order_id'=>$value['id']))->select();
				 $t_no=serialNumber('OtherInbound');//流水号
				 $order_no='OIM'.date('Ymd',time()).$t_no;//流水单号
				// echo $n->getlastsql();
				//推到其它入库单订单
				$otherInbound_order['order_no']=$order_no;
				$otherInbound_order['old_order_no']=$value['order_no'];//原单号
				$otherInbound_order['warehouse_code']=session('warehouse_code');//来源仓库编码
				$otherInbound_order['add_time']=time();
				$status_order=$ap_s->add($otherInbound_order);
				
				foreach($info_order_list as $k=>$v){
					$goods=$b->where(array('id'=>$v['goods_id']))->find();
					$bookscate=$c->where(array('code'=>$goods['manual_id']))->find();
					$books_goodsinfo=$d_s->where(array('manua_id'=>$goods['manual_id'],'goods_no'=>$goods['goods_no']))->find();
					if(!$books_goodsinfo){//没找到对应商品
						$ap_s->delete($status_order);//回滚操作
						$return['status']=0;
						$return['info']="对方仓库账册中没找到对应商品，请联系对方添加后再推送！";
						$this->ajaxReturn($return);
						die;
					}

					if($status_order){
							//推到其它入库订单详情
							$list['order_id']=$status_order;
							$list['books_id']=$books_goodsinfo['id'];//对应账册id
							$list['unit_count']=$v['nums'];//调拨数量
							$status=$a_s->add($list);//详情添加
					}
					if(!$status){
						$return['status']=0;
						$return['info']="添加失败";
						$this->ajaxReturn($return);
					}
				}
				M('MoveDepot',C('DB_PRS'),C('DB_DEPOT'))->where(array('id'=>$value['id']))->setField('push_inbound_status',1);
				$return['status']=1;
				$return['info']="推送成功！";
				$this->ajaxReturn($return);
			}else{
				$return['status']=0;
				$return['info']="已推送过了";
				$this->ajaxReturn($return);
			}
			
		}
		$this->ajaxReturn($return);
    }
	//推出库单(仓库调拨)
    /* public function pushDepotOut(){
		//推送到货通知单
		$m=M('MoveDepot',C('DB_PRS'),C('DB_DEPOT'));
		$n=M('MoveDepotInfo',C('DB_PRS'),C('DB_DEPOT'));
		$b=M('Goods',C('DB_PRS'),C('DB_DEPOT'));
		$c=M('BooksCate',C('DB_PRS'),C('DB_DEPOT'));
		$p=M('Pushorder',C('DB_PRS'),C('DB_DEPOT'));
		$g=M('Pushordergoods',C('DB_PRS'),C('DB_DEPOT'));
		
		$id=I('id');

		$ids=explode(',',$id);
		$map['id']=array('in',$ids);
		$list_order=$m->where($map)->select();
		
		foreach($list_order as $key=>$value){
			if(empty($value['examine_time'])){
				$return['status']=0;
				$return['info']="未审核不能推送";
				$this->ajaxReturn($return);
			}
			if(!$value['push_status']){//未推送
				$pushorder['order_no']=$value['order_no'];
				$pushorder['user_name']='其它出库';
				$res=$p->add($pushorder);
				if($res){
					$info_order_list=$n->where(array('order_id'=>$value['id']))->select();
					// echo $n->getlastsql();
					foreach($info_order_list as $k=>$v){
						$goods=$b->where(array('id'=>$v['goods_id']))->find();
						$bookscate=$c->where(array('code'=>$goods['manua_id']))->find();
						// echo $b->getlastsql();
						// echo $c->getlastsql();
						// die;
						//推到到货通知表
						$list['order_id']=$res;
						$list['goods_no']=$goods['goods_no'];
						$list['goods_name']=$goods['goods_name'];
						$list['goods_nums']=$v['unit_count'];
						$list['goods_price']=$goods['goods_price'];//价格
						$list['addtime']=time();
						$status=$g->add($list);//到货通知单添加
						// echo $g->getlastsql();
						// die;
						if(!$status){
							$return['status']=0;
							$return['info']="推送订单详情信息失败";
							$this->ajaxReturn($return);
						}
					}
					$m->where(array('id'=>$value['id']))->setField('push_status',1);
					$return['status']=1;
					$return['info']="推送成功";
					$this->ajaxReturn($return);
				}else{
					$return['status']=0;
					$return['info']="推送订单信息失败";
					$this->ajaxReturn($return);
				}
				
			}else{
				$return['status']=0;
				$return['info']="已经推送";
				$this->ajaxReturn($return);
			}
		}
		
		
		$this->ajaxReturn($return);
    } */
	
	
	public function packagematerialbooksExamine(){
		$return=tableExamine('PackageMaterialBooks');
        $this->ajaxReturn($return);
    }
	//审核库位
    public function examineStorage(){
		$return=tableExamine('Storage');
        $this->ajaxReturn($return);
    }
	//删除其它出库单
    public function otherOutboundDel(){
		$return=tableDel('OtherOutbound');
        $this->ajaxReturn($return);
    }
	//清空其它出库单回收站
    public function otherOutboundClear(){
		$return=tableClear('OtherOutbound');
        $this->ajaxReturn($return);
    }
	
	//恢复其它出库单回收站数据
    public function otherOutboundUndo(){
		$return=tableUndo('OtherOutbound');
        $this->ajaxReturn($return);
    }
	
	//恢复库位
    public function storageUndo(){
		$return=tableUndo('Storage');
        $this->ajaxReturn($return);
    }
	
	//其它入库详情
	public function otherInboundInfo(){
		$this->display();
    }
	
	//其它出库详情
	public function otherOutboundInfo(){
		$m=M('OutboundCate',C('DB_PRS'),C('DB_DEPOT'));
		$outbound_catelist=$m->where(array('status'=>1))->select();
		
		$this->assign('outbound_catelist',$outbound_catelist);
		$this->display();
    }
	
	//入库详情列表数据
	public function get_otherInboundInfo(){
		$m=M('OtherInboundInfo',C('DB_PRS'),C('DB_DEPOT'));
        $ck=A('CheckInput');
		//参数验证
        $page=$ck->in('当前页','page','intval','1',0,0);
        $rows=$ck->in('每页记录数','rows','intval','',0,0);
		//排序
        $sort=$ck->in('排序','sort','cnennumstr','id',0,0);
        $order=$ck->in('规则','order','cnennumstr','desc',0,0);
		$order_id=I('order_id');
		$map['m.order_id']=$order_id;
		$count=$m->alias('m')->join('__BOOKS__ as n on m.books_id=n.id')->where($map)->count();
        $info=$m->alias('m')->join('__BOOKS__ as n on m.books_id=n.id')->field('m.id,m.order_id,m.books_id,m.unit_count,m.price,m.conforming_p,m.nonconforming_p,m.scrap_one,n.goods_no,n.goods_name,n.goods_spec,n.current_type,n.source,n.unit1')->where($map)->page($page,$rows)->order($sort.' '.$order)->select();
		// echo $m->getlastsql();
		// die;
		// dump($info);
		//获取数据
        $data['total']=$count;
        $data['rows']=$info;
		
        $this->ajaxReturn($data);
    }
	
	//出库详情列表数据
	public function get_otherOutboundInfo(){
		$m=M('OtherOutboundInfo',C('DB_PRS'),C('DB_DEPOT'));
		$ao=M('ArrivalOrder',C('DB_PRS'),C('DB_DEPOT'));
        $ck=A('CheckInput');
		//参数验证
        $page=$ck->in('当前页','page','intval','1',0,0);
        $rows=$ck->in('每页记录数','rows','intval','',0,0);
		//排序
        $sort=$ck->in('排序','sort','cnennumstr','id',0,0);
        $order=$ck->in('规则','order','cnennumstr','desc',0,0);
		$order_id=I('order_id');
		$map['m.order_id']=$order_id;
		
		$count=$m->alias('m')->join('__ARRIVAL_ORDER__ as n on m.goods_id=n.id')->where($map)->count();
        $info=$m->alias('m')->join('__ARRIVAL_ORDER__ as n on m.goods_id=n.id')->field('m.id,m.order_id,m.goods_id,m.unit_count,m.outbound_cate_no,n.goods_no,n.manual_id as manua_id,n.goods_name,n.goods_spec,n.currency as current_type,n.country_code,n.unit1')->where($map)->page($page,$rows)->order($sort.' '.$order)->select();
		// echo $m->getlastsql();
		// dump($info);
		//获取数据
        $data['total']=$count;
        $data['rows']=$info;
		
        $this->ajaxReturn($data);
    }
	//保存其它入库详情编辑数据
	public function materialInfoSave(){
		$m=M('PackageMaterialOrderinfo',C('DB_PRS'),C('DB_DEPOT'));
        if(!IS_POST) exit;
        $ck=A('CheckInput');
		$data['nums']=I('unit_count');
        $map['id']=$ck->in('id','id','intval','',1,0);
		
        $result=$m->where($map)->save($data);
		
        if(false===$result){
            $return['message']='更新信息失败';
            $return['status']=false;
            $this->ajaxReturn($return);
        }else{
            $return['message']='更新信息成功!';
			$return['status']=true; 
			$this->ajaxReturn($return);
        }
	}
	//保存详情编辑数据
	public function moveDepotInfoSave(){
		$m=M('MoveDepotInfo',C('DB_PRS'),C('DB_DEPOT'));
		$n=M('MoveDepot',C('DB_PRS'),C('DB_DEPOT'));
		$ao=M('ArrivalOrder',C('DB_PRS'),C('DB_DEPOT'));
        if(!IS_POST) exit; 
        $ck=A('CheckInput');
		$data['nums']=I('nums');
        $map['id']=$ck->in('id','id','intval','',1,0);
		
		$md_info=$m->where($map)->find();//查看调拨详情信息
		$ao_info=$ao->where(array('id'=>$md_info['goods_id']))->find();
		$storage_nums=$ao_info['storage_amount'];//库存数量
		if($data['nums']>$storage_nums){
			$return['message']='此数量大于库存数量不能保存';
            $return['status']=false;
            $this->ajaxReturn($return);
		}
        $result=$m->where($map)->save($data);
		// echo $m->getlastsql();
        if(false===$result){
            $return['message']='更新信息失败';
            $return['status']=false;
            $this->ajaxReturn($return);
        }else{
			$order=$m->where($map)->find();
			$n->where(array('id'=>$order['order_id']))->setField('status',0);
            $return['message']='更新信息成功!';
			$return['status']=true; 
			$this->ajaxReturn($return);
        }
	}
	//保存其它入库详情编辑数据
	public function otherInboundInfoSave(){
		$m=M('OtherInboundInfo',C('DB_PRS'),C('DB_DEPOT'));
        if(!IS_POST) exit; 
        $ck=A('CheckInput');
		$data['unit_count']=I('unit_count');
		$data['price']=I('price');
		$data['conforming_p']=I('conforming_p');
		$data['nonconforming_p']=I('nonconforming_p');
		$data['scrap_one']=I('scrap_one');
        $map['id']=$ck->in('id','id','intval','',1,0);
		
        $result=$m->where($map)->save($data);
		// echo $m->getlastsql();
		// die;
        if(false===$result){
            $return['message']='更新信息失败';
            $return['status']=false;
            $this->ajaxReturn($return);
        }else{
            $return['message']='更新信息成功!';
			$return['status']=true; 
			$this->ajaxReturn($return);
        }
	}
	//保存其它出库详情编辑数据--
	public function otherOutboundInfoSave(){
		$m=M('OtherOutboundInfo',C('DB_PRS'),C('DB_DEPOT'));
		$n=M('OtherOutbound',C('DB_PRS'),C('DB_DEPOT'));
		$ao=M('ArrivalOrder',C('DB_PRS'),C('DB_DEPOT'));
        if(!IS_POST) exit; 
        $ck=A('CheckInput');
		$data['unit_count']=I('unit_count');
		$data['outbound_cate_no']=I('outbound_cate_no');
        $map['id']=$ck->in('id','id','intval','',1,0);
		
		$ot_info=$m->where($map)->find();//查看详情信息
		
		$n_info=$n->where(array('id'=>$ot_info['order_id']))->find();
		if($n_info['out_status']){
			$return['message']="已出库不能再编辑商品！";
			$return['status']=false;
			$this->ajaxReturn($return);
		}
		
		$ao_info=$ao->where(array('id'=>$ot_info['goods_id']))->find();
		$storage_nums=$ao_info['storage_amount'];//库存数量
		if($data['unit_count']>$storage_nums){
			$return['message']='此数量大于库存数量不能保存';
            $return['status']=false;
            $this->ajaxReturn($return);
		}
		
        $result=$m->where($map)->save($data);
		
        if(false===$result){
            $return['message']='更新信息失败';
            $return['status']=false;
            $this->ajaxReturn($return);
        }else{
			$order=$m->where($map)->find();
			$n->where(array('id'=>$order['order_id']))->setField('status',0);//更新审核状态
			// echo $n->getlastsql();
            $return['message']='更新信息成功!';
			$return['status']=true; 
			$this->ajaxReturn($return);
        }
	}
	//删除其它入库详情编辑数据
	public function otherInboundInfoDel(){
		$m=M('OtherInboundInfo',C('DB_PRS'),C('DB_DEPOT'));
		$n=M('OtherInbound',C('DB_PRS'),C('DB_DEPOT'));
        if(!IS_POST) exit;
        $ck=A('CheckInput');  
        $id=$ck->in('id','id','string','',1,0);
        if(empty($id)) exit;//为空
        $arr=explode(',', $id);
        $map['id']=array('in',$arr);
		foreach($arr as $k=>$v){
			$info=$m->where(array('id'=>$v))->find();
			$order_info=$n->where(array('id'=>$info['order_id']))->find();
			if($order_info['status']==1){
				$return['message']='已审核通过，不能删除!';
				$return['status']=false; 
				$this->ajaxReturn($return);
			}
		}
        $result=$m->where($map)->delete();
        if(!$result){
            $return['message']='删除失败!';
            $return['status']=false;  
        }else{
            $return['message']='删除成功!';
            $return['status']=true; 
        }
        $this->ajaxReturn($return);
    }
	//删除其它出库详情编辑数据--
	public function otherOutboundInfoDel(){
		$m=M('OtherOutboundInfo',C('DB_PRS'),C('DB_DEPOT'));
		$n=M('OtherOutbound',C('DB_PRS'),C('DB_DEPOT'));
        if(!IS_POST) exit;
        $ck=A('CheckInput');  
        $id=$ck->in('id','id','string','',1,0);
        if(empty($id)) exit;//为空
        $arr=explode(',', $id);
		foreach($arr as $k=>$v){
			$info=$m->where(array('id'=>$v))->find();
			$order_info=$n->where(array('id'=>$info['order_id']))->find();
			// echo $n->getlastsql();
			// die;
			if($order_info['status']==1){
				$return['message']='已审核通过，不能删除!';
				$return['status']=false; 
				$this->ajaxReturn($return);
			}
		}
        $map['id']=array('in',$arr);
        $result=$m->where($map)->delete();
		// echo $m->getlastsql();
        if(!$result){
            $return['message']='删除失败!';
            $return['status']=false;  
        }else{
             $return['message']='删除成功!';
             $return['status']=true; 
        }    
        $this->ajaxReturn($return);
    }
	//删除调拨单详情编辑数据--
	public function moveDepotInfoDel(){
		$m=M('MoveDepotInfo',C('DB_PRS'),C('DB_DEPOT'));
		$n=M('MoveDepot',C('DB_PRS'),C('DB_DEPOT'));
        if(!IS_POST) exit;
        $ck=A('CheckInput');  
        $id=$ck->in('id','id','string','',1,0);
        if(empty($id)) exit;//为空
        $arr=explode(',', $id);
		foreach($arr as $k=>$v){
			$info=$m->where(array('id'=>$v))->find();
			$order_info=$n->where(array('id'=>$info['order_id']))->find();
			// echo $n->getlastsql();
			// die;
			if($order_info['status']==1){
				$return['message']='已审核通过，不能删除!';
				$return['status']=false; 
				$this->ajaxReturn($return);
			}
		}
        $map['id']=array('in',$arr);
        $result=$m->where($map)->delete();
		// echo $m->getlastsql();
        if(!$result){
            $return['message']='删除失败!';
            $return['status']=false;  
        }else{
             $return['message']='删除成功!';
             $return['status']=true; 
        }
        $this->ajaxReturn($return);
    }
	//耗材入库
    public function material_inbound(){
		$m=M('PackageMaterialOrder',C('DB_PRS'),C('DB_DEPOT'));
		$n=M('PackageMaterialOrderinfo',C('DB_PRS'),C('DB_DEPOT'));
		$g=M('PackageMaterial',C('DB_PRS'),C('DB_DEPOT'));
		$b=M('PackageMaterialBooks',C('DB_PRS'),C('DB_DEPOT'));
        if(!IS_POST) exit;
        $ck=A('CheckInput');
		$id=$ck->in('入库单id','id','string','',1,0);
        if(empty($id)) exit;//为空
        $arr=explode(',', $id);
		$map['id']=array('in',$arr);
		$has=$m->where($map)->select();
		
		foreach($has as $k=>$v){
			if($v['inbound_status']==1){
				$return['message']='单号"'.$v['order_no'].'"已经入库!';
				$return['status']=false;    
				$this->ajaxReturn($return);//已经入库
			}
			$list_info=$n->where(array('order_id'=>$v['id']))->select();
			foreach($list_info as $key=>$value){
				$nums=$value['nums'];//入库数量
				$books=$b->where(array('id'=>$value['books_id']))->find();
				$goods=$g->where(array('pn'=>$books['pn']))->find();
				if($goods){//库存中已经有此耗材，则更新库存数据
					$data['quantity']=array('exp',"quantity+$nums");
					$ress=$g->where(array('pn'=>$books['pn']))->save($data);
					// echo $g->getlastsql();
					// dump($goods);
					// die;
					if($ress!==false){
						$m->where(array("id"=>$v['id']))->setField('inbound_status',1);//更新入库状态
						continue;
					}else{
						$return['message']=$v['order_no'].'| 入库失败！';
						$return['status']=false;    
						$this->ajaxReturn($return);//已经入库
					}
				}else{	//库存中没有此耗材,则添加此耗材数据入库存
					$data['name']=$books['name'];
					$data['pn']=$books['pn'];
					$data['quantity']=$nums;
					$data['l_w_h']=$books['l_w_h'];

					$resss=$g->add($data);
					// echo $n->getlastsql();
					if($resss){
						$m->where(array("id"=>$v['id']))->setField('inbound_status',1);//更新入库状态
						continue;
					}else{
						$return['message']=$v['order_no'].'| 入库失败！';
						$return['status']=false;    
						$this->ajaxReturn($return);
					}
				}
			}
		}

        $return['message']='入库成功!';
        $return['status']=true;    
        $this->ajaxReturn($return);
    }
	//编辑耗材确认
    public function editpackagematerial(){
		$m=M('PackageMaterial',C('DB_PRS'),C('DB_DEPOT'));
        if(!IS_POST) exit; 

		$data=I('param.','','trim');

        $map['id']=$data['id'];
		unset($data['id']);
		
        $result=$m->where($map)->save($data);

        if(false===$result){
            $return['message']='保存失败';
            $return['status']=false;
            $this->ajaxReturn($return);
        }else{
			$return['message']='保存成功!';
			$return['status']=true;
			$this->ajaxReturn($return);
        }
    }
	
	//盘库
	public function checkStock(){
		$t_no=serialNumber('Checkstock');//流水号
		$order_no='CS'.date('Ymd',time()).$t_no;//流水单号
		
		$sectionlist=sectionlist();
		$personnellist=personnellist();
		$contactsCompanylist=contactsCompanylist();
		$manuallist=manuallist();

		$this->assign('manuallist',$manuallist);
		$this->assign('sectionlist',$sectionlist);
		$this->assign('personnellist',$personnellist);
		$this->assign('contactsCompanylist',$contactsCompanylist);
		$this->assign('order_no',$order_no);
		
		$this->display();
    }
	//添加盘库单
    public function checkStockAdd(){
		$m=M('Checkstock',C('DB_PRS'),C('DB_DEPOT'));
		$n=M('CheckstockInfo',C('DB_PRS'),C('DB_DEPOT'));
		$g=M('Goods',C('DB_PRS'),C('DB_DEPOT'));
        if(!IS_POST) exit;
		
        $ck=A('CheckInput'); 
		$data['order_no']=I('order_no');
		$data['section_no']=I('section_no');
		$data['people_make_no']=I('people_make_no');
		$data['people_purchasing_no']=I('people_purchasing_no');
		$data['manua_id']=I('manua_id');
		$data['add_time']=time();
		$data['update_time']=time();
		
		//重复判断
		$map['order_no']=I('order_no');
		$duplicate=$m->where($map)->find();
		if($duplicate){
			$return['message']='单号已经存在';
            $return['status']=false;
            $this->ajaxReturn($return);
		}
		//是否存在库存信息判断
		$manua_id=I('manua_id');
		if($manua_id=='All'){
			$list_goods=$g->alias('m')->field('m.*,sum(a.freez_amount) freez_amount,sum(a.storage_amount) storage_amount2')->join("__ARRIVAL_ORDER__ a on m.goods_no=a.goods_no and m.manua_id=a.manual_id","left")->group('m.goods_no,m.manua_id')->select();
		}else{
			$list_goods=$g->alias('m')->field('m.*,sum(a.freez_amount) freez_amount,sum(a.storage_amount) storage_amount2')->join("__ARRIVAL_ORDER__ a on m.goods_no=a.goods_no and m.manua_id=a.manual_id","left")->group('m.goods_no,m.manua_id')->where(array('m.manua_id'=>$manua_id))->select();
		}
		// echo $g->getlastsql();
		// die;
		if(count($list_goods)==0){
			$return['message']='此账册下没有查到商品库存信息，不能添加！';
			$return['status']=false;
			$this->ajaxReturn($return);
		}
        $insertId=$m->add($data);
        if(!$insertId){
            $return['message']='添加失败';
            $return['status']=false;
            $this->ajaxReturn($return);
        }else{
			//添加盘库详情信息
			foreach($list_goods as $k=>$v){
				if(empty($v['storage_amount2'])){
					$v['storage_amount2']=0;
				}
				if(empty($v['conforming_p'])){
					$v['conforming_p']=0;
				}
				if(empty($v['nonconforming_p'])){
					$v['nonconforming_p']=0;
				}
				if(empty($v['scrap_one'])){
					$v['scrap_one']=0;
				}
				$datas['manua_id']=$v['manua_id'];
				$datas['source_no']=$v['source_no'];
				$datas['goods_name']=$v['goods_name'];
				$datas['goods_no']=$v['goods_no'];
				$datas['goods_spec']=$v['goods_spec'];
				$datas['amount']=$v['storage_amount2'];
				$datas['bar_code']=$v['bar_code'];
				$datas['conforming_p']=$v['conforming_p'];
				$datas['nonconforming_p']=$v['nonconforming_p'];
				$datas['scrap_one']=$v['scrap_one'];
				$datas['order_id']=$insertId;
				$status=$n->add($datas);
				// echo $n->getDbError();

				if(!$status){
					$return['message']='添加盘库详情失败!';
					$return['status']=false; 
					$this->ajaxReturn($return);
				}
			}
            $return['message']='添加成功!';
			$return['status']=true; 
			$this->ajaxReturn($return);
        }
    }
	//更新盘库单
    public function checkStockSave(){
		$m=M('Checkstock',C('DB_PRS'),C('DB_DEPOT'));
        if(!IS_POST) exit; 
        $ck=A('CheckInput');
		$data['section_no']=I('section_no');
		$data['people_make_no']=I('people_make_no');
		$data['people_purchasing_no']=I('people_purchasing_no');
		$data['manua_id']=I('manua_id');
		$data['update_time']=time();
        $map['id']=$ck->in('id','id','intval','',1,0);
		
        $result=$m->where($map)->save($data);
        if(false===$result){
            $return['message']='更新信息失败';
            $return['status']=false;
            $this->ajaxReturn($return);
        }else{
            $return['message']='更新信息成功!';
			$return['status']=true; 
			$this->ajaxReturn($return);
        }
    }
	//审核其它出库单
    public function examineCheckStock(){
		$m=M('Checkstock',C('DB_PRS'),C('DB_DEPOT'));
		$n=M('CheckstockInfo',C('DB_PRS'),C('DB_DEPOT'));
        if(!IS_POST) exit;  
        $id=I('id');
		$value=I('value');
        if(empty($id)) exit;//为空
        $arr=explode(',', $id);
        $map['id']=array('in',$arr);
		if($value==1){
			$data['status']=1;
		}else if($value==2){
			$data['status']=2;
		}else{
			$return['message']='审核状态参数错误!';
            $return['status']=false;
			$this->ajaxReturn($return);
			exit;
		}
		$data['examine_time']=time();
		$m_list=$m->where($map)->select();
		foreach($m_list as $ks=>$vs){
			if($vs['status']==1){
				$return['message']='已审核通过不能修改!';
				$return['status']=false;
				$this->ajaxReturn($return);
			}
		}
        $result=$m->where($map)->save($data);
		// echo $m->getlastsql();
		// die;
        if(!$result){
            $return['message']='审核失败!';
            $return['status']=false;  
        }else{
			 $url=C('base_config.push_checkstockinfo');
			 
			 foreach($m_list as $k=>$v){
				 if(!empty($v['erp_code'])){
					 $n_list=$n->where(array('order_id'=>$v['id']))->select();
					 $send_data['erp_code']=$v['erp_code'];
					 $send_data['detail']=$n_list;
					 $send_data['order_no']=$v['old_order_no'];
					 $send_res=push($url,$send_data);
					 $send_res=json_decode($send_res,true);
					 // dump($send_data);
					 // die;
					 if(!$send_res['status']){
						 $return['message']='同步盘库信息到erp失败!';
						 $return['status']=false; 
						 $this->ajaxReturn($return);
					 }
				 }
			 }
             $return['message']='审核成功!';
             $return['status']=true; 
        }
        $this->ajaxReturn($return);
    }
	//删除其它出库单
    public function checkStockDel(){
		$return=tableDel('Checkstock');
        $this->ajaxReturn($return);
    }
	//清空其它出库单回收站
    public function checkStockClear(){
		$return=tableClear('Checkstock');
        $this->ajaxReturn($return);
    }
	//恢复其它出库单回收站数据
    public function checkStockUndo(){
		$return=tableUndo('Checkstock');
        $this->ajaxReturn($return);
    }
	//盘库单列表数据
	public function get_checkstock(){
        $ck=A('CheckInput');
		//参数验证
        $order_no=$ck->in('单据编号','order_no','ennumstr','',0,0);
		$manua_id=$ck->in('账册编号','manua_id','ennumstr','',0,0);
        
        $page=$ck->in('当前页','page','intval','1',0,0);
        $rows=$ck->in('每页记录数','rows','intval','',0,0);
		$status=I('status');
		//排序
        $sort=$ck->in('排序','sort','cnennumstr','id',0,0);
        $order=$ck->in('规则','order','cnennumstr','desc',0,0);
		//搜索条件
		$map=array();
		
		!empty($order_no)?$map['order_no']=array('like','%'.$order_no.'%'):'';
		!empty($manua_id)?$map['manua_id']=array('like','%'.$manua_id.'%'):'';
		//获取数据
        $data=getTable('Checkstock',$map,$page,$rows,$sort,$order,$status);
		
        $this->ajaxReturn($data);
    }
	//盘库单列表数据
	public function checkStockInfo(){
		$this->display();
	}
	//库位
	public function storage(){
		$this->display();
	}
	//库存商品列表
	public function get_checkStockInfo(){
        $m=M('CheckstockInfo',C('DB_PRS'),C('DB_DEPOT'));
        $ck=A('CheckInput');

        //搜索条件
        $map=array();
        $goods_no=$ck->in('商品编码','goods_no','ennumstr','',0,0);        
        $goods_name=$ck->in('商品名称','goods_name','cnennumstr','',0,0);
		
		$order_id=I('order_id');
		if(empty($order_id)){
			$this->error('没有单号');
		}else{
			$map['order_id']=$order_id;
		}
		
		!empty($goods_no)?$map['goods_no']=array('like','%'.$goods_no.'%'):'';
		!empty($goods_name)?$map['goods_name']=array('like','%'.$goods_name.'%'):'';

        $page=$ck->in('当前页','page','intval','1',0,0);   
        $rows=$ck->in('每页记录数','rows','intval','',0,0);  

        //排序
        $sort=$ck->in('排序','sort','cnennumstr','id',0,0);
        $order=$ck->in('规则','order','cnennumstr','desc',0,0);
        
        $count=$m->where($map)->count();
        if(empty($map)){
            $info=$m->page($page,$rows)->order($sort.' '.$order)->select();
        }else{
            $info=$m->where($map)->page($page,$rows)->order($sort.' '.$order)->select();
        }
		foreach($info as $k=>$v){
			$info[$k]['push_time']=date("Y-m-d H:i:s",$v['push_time']);
			if($v['amount']!=$v['check_amount']){
				// $info[$k]['amount']='*';
				$info[$k]['color']=0;
			}
			if($v['conforming_p']!=$v['check_conforming_p']){
				// $info[$k]['conforming_p']='*';
			}
			if($v['nonconforming_p']!=$v['check_nonconforming_p']){
				// $info[$k]['nonconforming_p']='*';
			}
			if($v['scrap_one']!=$v['check_scrap_one']){
				// $info[$k]['scrap_one']='*';
			}
		}
        if(!empty($info)){
            $data['total']=$count;
            $data['rows']=$info;
        }else{
            $data['total']=0;
            $data['rows']=0;
        }
		
        $this->ajaxReturn($data);
    }
	
	//编辑商品确认
    public function confirm_editcheckStockInfo(){
		$m=M('CheckstockInfo',C('DB_PRS'),C('DB_DEPOT'));
        if(!IS_POST) exit; 

		$data=I('param.','','trim');

        $map['id']=$data['id'];
		unset($data['id']);
		
        $result=$m->where($map)->save($data);

        if(false===$result){
            $return['message']='保存失败';
            $return['status']=false;
            $this->ajaxReturn($return);
        }else{
			$return['message']='保存成功!';
			$return['status']=true;
			$this->ajaxReturn($return);
        }
    }
	
	//其它出库-出库
	public function otherOutboundout(){
		$so=M('StorageOutbound',C('DB_PRS'),C('DB_DEPOT'));
		$mm=M('OtherOutbound',C('DB_PRS'),C('DB_DEPOT'));
		$mmi=M('OtherOutboundInfo',C('DB_PRS'),C('DB_DEPOT'));
		$ao=M('ArrivalOrder',C('DB_PRS'),C('DB_DEPOT'));
		$id=I('id');

		$arr=explode(',',$id);
		foreach($arr as $k=>$v){
			$info=$mm->where(array('id'=>$v))->find();
			if($info['status']!=1){
				$return['info']='未审核通过，不能出库!';
				$return['status']=false; 
				$this->ajaxReturn($return);
				die;
			}
			if($info['out_status']==1){
				$return['info']='已出库，不能再次出库!';
				$return['status']=false;
				$this->ajaxReturn($return);
				die;
			}
			$order_list=$mmi->where(array('order_id'=>$v))->select();
			foreach($order_list as $ks=>$vs){
				$map_ao['id']=$vs['goods_id'];
				$data_ao['freez_amount']=array('exp','freez_amount-'.$vs['unit_count']);//释放冻结数量
				$ao_info=$ao->where($map_ao)->find();//取得当前批次详情信息
				$ao->where($map_ao)->save($data_ao);//更新

				$so->add(array('outbound_order_id'=>$info['id'],'inbound_order_id'=>$vs['goods_id'],'nums'=>$vs['unit_count'],'add_time'=>time(),'out_cate'=>6));//更新出库记录out_cate:(出库类型1:订单出库2:异常出库3:销售单出库数量4:调拨出库5:领料出库6:其它出库)
			}
			$mm->where(array('id'=>$v))->setField('out_status',1);//更新出库状态
		}
		//按企业编码扣费--begin
		// out_money(19,1,16);//19：退货商品清点
		// out_money(20,1,16);//20：客户退货验收入库
		// out_money(21,1,16);//21：客户退货上架
		//扣费--end
		$return['info']='出库成功!';
		$return['status']=true;
		$this->ajaxReturn($return);
	}

	//出库申报
	public function send_otheroutbound(){
		$m=M('Pushorder',C('DB_PRS'),C('DB_DEPOT'));
		$pushapi = A('Pushsendapi'); 
		$res=$pushapi->get_methodbatch_otheroutbound();//出库申报
		$this->ajaxReturn($res);
	}

    //不良品设置
    public function bad_product(){
	    //审核权限 方法名：examineCheckstockbad auth_rule id:390
        $groupid = M('AuthGroupAccess',C('DB_PRS'),C('DB_DEPOT'))->where(array('uid'=>session('uid')))->find();
        $arr = M('AuthGroup',C('DB_PRS'),C('DB_DEPOT'))->field('rules')->where(array('id'=>$groupid['group_id']))->find();
        $examine = false;
        if(in_array('390',explode(',',$arr['rules']))){
            $examine = true;
        }
        $this->assign('exmaine',$examine);
        $this->display();
    }
    //不良品列表
    public function get_bad_product(){
        $ck=A('CheckInput');
        $ao=M('ArrivalOrder',C('DB_PRS'),C('DB_DEPOT'));
        //参数验证
        $param=I('param.','','trim');
        $goods_name=$param['goods_name'];//商品名称
        $goods_no=$param['goods_no'];//商品编码

        $page=$ck->in('当前页','page','intval','1',0,0);
        $rows=$ck->in('每页记录数','rows','intval','',0,0);
        //排序
        $sort=$ck->in('排序','sort','cnennumstr','id',0,0);
        $order=$ck->in('规则','order','cnennumstr','desc',0,0);
        //搜索条件
        $map=array();

        $map['cate']=1;//1入库2出库
        !empty($goods_name)?$map['goods_name']=array('like','%'.$goods_name.'%'):'';
        !empty($goods_no)?$map['goods_no']=array('like','%'.$goods_no.'%'):'';
        $status=I('status');
        //获取数据
        $data=getTable('CheckstockBadproduct',$map,$page,$rows,$sort,$order,$status);
        foreach($data['rows'] as $k=>$v){
            $data['rows'][$k]['goods_order_no']=$ao->where(array('id'=>$v['goods_id']))->getField('order_no');

        }

        $this->ajaxReturn($data);
    }
    public function checkstockbadSave(){
        $m=M('CheckstockBadproduct',C('DB_PRS'),C('DB_DEPOT'));
        if(!($fileid = $_GET['fileid'])){
            if(!IS_POST) exit;
        }
        $param=I('param.','','trim');
        $status				=$param['status'];
        $id					=$param['id'];
        $data['goods_nums']	=$param['goods_nums'];
        $data['mark']		=$param['mark'];
        $map['id']=$id;
        if($fileid && empty($_FILES)){$this->ajaxReturn(array('message'=>'没有文件','status'=>0));}
        if (!empty($_FILES)){
            $config = array(
                'maxSize'    =>    3145728,
                'savePath'   =>    '/Import/',
                'saveName'   =>    array('uniqid',''),
                'exts'       =>    array('jpg','jpeg','png','gif'),
                'autoSub'    =>    true,
                'subName'    =>    array('date','Ymd'),);
            $upload = new \Think\Upload($config);// 实例化上传类
            $info   =   $upload->upload();
            if(!$info) {// 上传错误提示错误信息
                $this->ajaxReturn(array('message'=>$upload->getError(),'status'=>0));
                //$this->error();
            }
            $extension=$info['goods_img']['ext'];
            $file_name='./Uploads'.$info['goods_img']['savepath'].$info['goods_img']['savename'];
            $arr = array('id'=>$fileid,'goods_img'=>$file_name);
            $ret=$m->save($arr);
            if($ret!==false){$this->ajaxReturn(array('message'=>'上传成功','status'=>1));}else{$this->ajaxReturn(array('message'=>'上传失败','status'=>0));}
        }

        $m_info=$m->where($map)->find();
        if($m_info['status']==1){
            alert_json(0,'已审核通过不能更改！');
        }

        $res=$m->where($map)->save($data);
        if($res===false){
            $return['message']='编辑失败';
            $return['status']=0;
            $this->ajaxReturn($return);
        }else{
            $return['message']='编辑成功!';
            $return['status']=1;
            $this->ajaxReturn($return);
        }
    }
    public function checkstockbadAdd(){
        $m=M('CheckstockBadproduct',C('DB_PRS'),C('DB_DEPOT'));
        $ao=M('ArrivalOrder',C('DB_PRS'),C('DB_DEPOT'));
        if(!IS_POST) exit;
        $param=I('param.','','trim');
        $ids=$param['ids'];
        $ids=explode(',',$ids);
        $map_ao['id']=array('in',$ids);
        $ao_list=$ao->field('id,goods_no,goods_name,order_no')->where($map_ao)->select();

        $t_no=serialNumber('CheckstockBadproduct');//流水号
        $order_no='CL'.date('Ymd',time()).$t_no;//流水单号

        foreach($ao_list as $k=>$v){
            $data[$k]['goods_id']=$v['id'];
            $data[$k]['goods_no']=$v['goods_no'];
            $data[$k]['goods_name']=$v['goods_name'];
            $data[$k]['order_no']=$order_no;
            $data[$k]['add_time']=time();
            $data[$k]['cate']=$param['cate'];
        }
        $insert=$m->addAll($data);
        if(!$insert){
            $return['message']='添加失败';
            $return['status']=0;
            $this->ajaxReturn($return);
        }else{
            $return['message']='添加成功!';
            $return['status']=1;
            $this->ajaxReturn($return);
        }
    }
    public function checkstockbadDel(){
        $m=M('CheckstockBadproduct',C('DB_PRS'),C('DB_DEPOT'));
        $id=I('id');
        $arr=explode(',',$id);
        $map['id']=array('in',$arr);
        $m_list=$m->field('status')->where($map)->select();
        if(deep_in_array(1,$m_list)){
            alert_json(0,'已审核通过不能删除！');
        }
        $return=tableDel('CheckstockBadproduct');
        $this->ajaxReturn($return);
    }
    //不良品审核
    public function examineCheckstockbad(){
        $m=M('CheckstockBadproduct',C('DB_PRS'),C('DB_DEPOT'));
        $ao=M('ArrivalOrder',C('DB_PRS'),C('DB_DEPOT'));
        $id=I('id');
        $value=I('value');
        $arr=explode(',',$id);
        $map['id']=array('in',$arr);
        $m_list=$m->field('status')->where($map)->select();
        if(deep_in_array(1,$m_list)){
            alert_json(0,'已审核通过不能再次审核！');
        }
        //审核通过，更新库存
        if($value==1){
            $trans = M('',C('DB_PRS'), C('DB_DEPOT'));
            $trans->startTrans();   // 开启事务
            $m_list=$m->field('goods_id,cate,goods_nums,status')->where($map)->select();
            foreach($m_list as $k=>$v){
                $map_ao['id']=$v['goods_id'];
                if(empty($v['goods_nums'])){
                    alert_json(0,'请先填写数量再审核！');
                }
                if($v['cate']==1){
                    // $data_ao['freez_amount']=array('exp','freez_amount-'.$v['goods_nums']);
                    $data_ao['bad_product']=array('exp','bad_product+'.$v['goods_nums']);
                }else{
                    // $data_ao['freez_amount']=array('exp','freez_amount+'.$v['goods_nums']);
                    $data_ao['bad_product']=array('exp','bad_product-'.$v['goods_nums']);
                }

                $res_ao=$ao->where($map_ao)->save($data_ao);
                if($res_ao===false){
                    $trans->rollback();
                    alert_json(0,'失败');
                }
            }
            $trans->commit();
        }
        $return=tableExamine('CheckstockBadproduct');
        $this->ajaxReturn($return);
    }
	
	
	
	
	
	
	
	
	
	
	
}